Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chrispian/df046f5718e8f413f8fafe08bf863f57 to your computer and use it in GitHub Desktop.
Save chrispian/df046f5718e8f413f8fafe08bf863f57 to your computer and use it in GitHub Desktop.
Only shows related products from the primary category set by Yoast
@JapeNZ
Copy link

JapeNZ commented Feb 21, 2021

Hi there,
This is awesome!

Would there be a way to alter the code to use the default Woocommerce category if no Yoast Primary Category is selected do you think?

The vast majority of my products are in multiple categories and this code works perfectly, the only problem being that for the products that are only in a single category, and therefore don't have a primary, no related products are shown.

Also would there a way to set it up so that if there are less than 4 products found it uses the default Woocommerce category?

As it is it doesn't seem to show variable products (though this may be because I'm using the WooCommerce Show Single Variations Plugin, that sets variable products as 'hidden'), I'm seeing a few instances where it's only showing a couple of related products... defaulting to the standard Woocommerce category for related products when less than 4 products are found in the Yoast Primary Category would fix this I'm hoping.

Any suggestions on how I might be able to get this to work would be very much appreciated!

Kind regards,
JP

@Caffe1neAdd1ct
Copy link

Latest version of woocommerce this needed a slight adjustment:

'excluded_ids'   => array( $product_id ),

should now be

'exclude'   => array( $product_id ),

Other than that still works fine, thanks for the gist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment