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
.