Skip Navigation

[Résolu] Filter Products base on Custom User Fields on Archive Product Page (WooCommerce)

This support ticket is created Il y a 3 années et 7 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 6 réponses, has 2 voix.

Last updated by Minesh Il y a 3 années et 7 mois.

Assigned support staff: Minesh.

Auteur
Publications
#1198995

I created a custom user field called Web Category ( checkbox ), and what I want is to show the products base on the product category and the "Web Category" user field.

Ex.
Current Logged-in User has selected the "Art" checkbox then the Views will filter the product list to only show the Art Product Category.

=== This was Resolved (https://toolset.com/forums/topic/filter-products-base-on-custom-user-fields/) but I would like to know if it is possible that we could do the same functionality on the Archive Page (Product)?

Thanks

#1199041

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - can you please share access details so I can check and see if we can apply the same thing on archive page.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#1199593

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I do not see you setup product archive yet as I do not see any archive:
=> hidden link

Can you please setup your product archive first on your site - please check the following Doc that will help you to understand how you can create the post type archive:
=> https://toolset.com/documentation/user-guides/creating-wordpress-custom-post-archives/

#1200480

Hi Minesh,

Done adding the product archive views.

Thanks

#1200499

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I've added the following code to Toolset's custom code section:

function func_filter_archive_by_category($query) {
  global $current_user;
  if (!is_admin() and is_post_type_archive('product') ) {
    $cat_slugs = array();
    
   $cats = get_user_meta($current_user->ID,'wpcf-web-category');
  if(isset($cats[0])){
   		foreach((array)$cats[0] as $k=>$v):
      		$cat_slugs[] = $v[0]; 
   		endforeach;
  } 
    
  	$taxquery = array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'slug',
            'terms' => $cat_slugs,
            'operator'=> 'IN'
        )
    );

    $query->set( 'tax_query', $taxquery );
  }
}
add_action( 'pre_get_posts', 'func_filter_archive_by_category');

=> hidden link

I can see the archive is respecting the category terms you selected:
=> hidden link

More info:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

Can you please confirm - it works at your end as well 🙂

#1202331

Hi Minesh,

Could you also do this on the Product Category Archive (Taxonomy)?.

Thanks

#1202347

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

Would you mind to create a new ticket and assign it to me and feel free to close this ticket.