Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hi Paul,

    Those filters from CommerceGurus are full of code smells and will cause unexpected behavior. I recommend using none of that as a template to work off.

    If I understand correctly, do you want the “People also viewed/bought” list to show only products pertaining to the current “Primary Product Category”?

    What do you want to use to output those ‘Product cards’ under “People also bought”? Product Catalog? Products by Category widget/block? Something else?

    What needs to happen is filtering the query arguments for that, where the product must have the primary term. So, the ‘option to display the product categories within which the item belongs’ that CommerceGurus mentions should have a way of limiting the categories requested. However, I’m not sure what they are referring to. So, could you please elaborate on how you output these?

    Once we know that, we get the Primary Term (Product Cat) from TSF via this method:

    tsf()->data()->plugin()->post()->get_primary_term( $product_id, 'product-cat' );
    

    All we need to do is run function_exists( 'tsf' ), and the function will check for compatibility automatically.

    With all that in mind, this is what the filter ought to contain:

    if ( function_exists( 'tsf' ) ) {
    	$primary_term_id = tsf()->data()->plugin()->post()->get_primary_term( get_the_id(), 'product_cat' );
    }
    
    if ( ! empty( $primary_term_id ) ) {
    	// Change the product card query here.
    }
    

    I can provide the full code once I know what you use to output the product cards. For example, it might be the filter woocommerce_products_widget_query_args, or we need to fall back to woocommerce_product_object_query_args.

    Thread Starter P T

    (@pthomson87)

    Hi Sabyr,

    Thank you for the comprehensive response.

    To clarify…

    Currently I have products in numerous categories, a main category, and other secondary categories.

    All I’m looking to do, is anywhere where the product card is shown (on main shop/archive pages, in the recommended products section on single products etc) is to show only the main product category (not the secondary categories)

    https://pasteboard.co/luKDKsdeZSUj.jpg

    https://pasteboard.co/eo1PykFEJkOU.jpg

    Many thanks,

    Paul

    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Paul,

    Thank you for clarifying! I’m happy to hear I understood you correctly.

    Lastly, could you please answer this question? What do you use to output those ‘Product cards’?
    Product Catalog? Products by Category widget/block? Just the archive pages? Something else? A mix of these things?

    This will help me narrow down which filter is best used. Once I know that, I can probably get back to you with a working filter snippet this evening.

    Thread Starter P T

    (@pthomson87)

    Hi Sybre,

    Thanks for this.

    To output the ‘Product Cards’ The homepage I use the WooCommerce blocks. The main shop page, is the standard archive pages, and the product pages are (i believe) the built in cross-sells and upsells. So would need the filter to work for all scenarios.

    All the best,

    Paul

    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Paul,

    It took a bit longer than anticipated. This is because WooCommerce doesn’t appear to use a standard way of querying products. They sometimes bypass the entire WordPress Posts API and query the database directly. I was trying to understand how to create a catch-all, but this appears impossible.

    In any case, we can only get the primary term for the current request or if we know the product ID of a particular request.

    I’m not sure how we would fit a primary term query on the homepage or main shop page. Could you elaborate on this? Neither the homepage nor the shop should have a specific term.

    We also cannot get the primary term for product archives — they should already show only products for that term.

    I believe all that’s left is the “related products” block/widget. Here’s the filter that changes its behavior to only display products from the primary term’s term:

    add_filter(
    	'woocommerce_get_related_product_cat_terms',
    	function ( $term_ids, $product_id ) {
    
    		$primary_term_id = function_exists( 'tsf' )
    			? tsf()->data()->plugin()->post()->get_primary_term_id( $product_id, 'product_cat' )
    			: null;
    
    		return $primary_term_id ? [ $primary_term_id ] : $term_ids;
    	},
    	10,
    	2,
    );

    (Where do I place filters?)

    After the filter is installed, you must clear your site’s transients — otherwise, the filter won’t work.

    The transient involved is named %wc_related_%, where % are wildcards. But other transients may be involved down the line, so it might be easiest to clear all your site’s transients using a plugin like this one.

    Please let me know if the filter works for you.

    Thread Starter P T

    (@pthomson87)

    Hi Sybre,

    Thanks for your efforts, but the filter still doesn’t seem to work. I’ve cleared the transients too and flushed all caches.

    Any other suggestions?

    Else, I’ll have to change all products to have only one category

    Many thanks,

    Paul

    • This reply was modified 5 months ago by P T.
    Plugin Author Sybre Waaijer

    (@cybr)

    Hi Paul,

    I think it’s best to contact the theme authors. If my suggested filter doesn’t work, they may have fiddled with the queries or have custom “related products” elements. It’s a closed-source theme, so I can only guess.

    I actually just wanted to modify one of the snippets you linked from their documentation, but none of them could even work at all (inoperable code). So, I’m not sure if they even tested them, as if they were just in theory and posted to fill up their docs with more content for SEO — perhaps even AI-generated. Moreover, the snippets are insecure. I wouldn’t want to burden you with a broken snippet.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.