WooCommerce: Custom Related Products

WooCommerce picks related products on the Single Product Page based on product categories and/or product tags. Related products are very important to the shopping experience, and sometimes this is not enough – what if you want to automatically show certain products based on different criteria?

So, here’s a quick snippet to e.g. get related products with the same product title of the current one. A very strange example, but you can use this as reference in case you want to get products based on different criteria.

The get_posts() function, in fact, can be customized to get products with a given stock, specific price range, same custom field value, search term, and so on.

In this screenshot, I’m getting Related Products by search title string = “Simple”. All products that include “Simple” in their title will be considered as related.

PHP Snippet: Custom Related Products @ WooCommerce Single Product Page

/**
 * @snippet       Get Related Products by Same Title @ WooCommerce Single
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.8
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_filter( 'woocommerce_related_products', 'bbloomer_related_products_by_same_title', 9999, 3 ); 

function bbloomer_related_products_by_same_title( $related_posts, $product_id, $args ) {
	$product = wc_get_product( $product_id );
	$title = $product->get_name();
	$related_posts = get_posts( array(
		'post_type' => 'product',
		'post_status' => 'publish',
		'title' => $title,
		'fields' => 'ids',
		'posts_per_page' => -1,
		'exclude' => array( $product_id ),
	));
	return $related_posts;
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

24 thoughts on “WooCommerce: Custom Related Products

  1. I used your snippet to put a related products tab on my product pages, and it worked great for the past two years.

    Now, for some reason, it still appears on the product pages and shows the names of the related products, but no longer the images.

    The links still work to the related products, but none of the related products tabs are showing the images.

    Probably due to some update somewhere.

    Is there any way to code the snippet to show the images?

    1. Hi Steve! This snippet just gives back a list of post IDs to the code, so images are output later on by WooCommerce itself. Either the returned IDs have no product featured image, or there is a plugin/theme conflict.

      To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentytwenty” theme (load the snippet there in functions.php) as explained here: https://www.businessbloomer.com/lesson/trwm4l01/

      Once you do that, does it work? If yes, you have a problem with your current theme or one of the plugins.

      Hope this helps!

  2. Dear Rodolfo
    Before..thank you for all your write and explain here.

    I found your code about : Custom Related Products @ WooCommerce Single Product Page.
    Since i like show the related product in single product page base of. the Brands i have.
    ( in other word is a book site and the brand is the name of the author ..i like show related product the other book of the same author )

    Can you help my in this. ?

    1. Hi Walter, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Good day. This code helped me a lot – thank you. I have a question. What is it possible for product names to be filtered down to the first “-” (dash) in the product name?
    greetings
    Marcin

    1. Hi Marcin, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. Hello! Do you have any idea of how I can customize the HTML output of the related products items?

    Best regards and thanks!

    1. Hi Miguel, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  5. Rodolfo, can I use this snippet to add similar products, located in nearby regions?

    I am looking for a solution to aggregate products only from certain regions or neighborhoods …

    In this case, you would have to place custom store address fields on each product. Put the city and neighborhood in each product … then search.

    1. Hi Joao, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. Hello. Your snippet doesn’t work because of ‘title’ argument. You must delete it. Also if someone disable random ordering, it can use add_filter( ‘woocommerce_product_related_posts_shuffle’, ‘__return_false’ ); filter. Thank you.

    1. Thanks – but do you get an actual error?

  7. Hi
    How can I display the latest related products in the product detail page? Currently, my related product page is out of date. Should need the way customers. What lines of code do you have to customize this?
    Thank you!

    1. Hi there, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  8. Not Working.

    Goodday, I included the snippet as per instruction. However the related products now disappeared completely.
    I am using elementor which is using the standard woocommerce related products.
    Hope you can help

    1. Hi Stephan, thanks for your comment! This snippets gets Related Products that have the same exact title as the current product – if it finds no products, Related Products will disappear.

      In case you’re looking for other case scenarios, I’m afraid that’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  9. Hi Rodolfo,
    is it possible show only in stock products in related products with this snippet? I tried to use “get_stock_status” instead “get_name” parameter but didn’t work. Can you give me some tip?

    1. Hello there, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  10. Hi there,

    I added your code to my functions.php file. But now nothing (no related products) are showing up..

    Any advice?

    1. Hi Steff! Did you customize my snippet or use the exact same?

  11. I’ve been subscribing and asking questions on some of the modifications but my posts have been awaiting moderation since Noah built the ark. This is a nice site with lots of information but useless for getting questions answered.

    1. Hey Cor, sorry about that. I was off for 3 weeks plus it usually takes me 2 weeks to reply. I do my best 🙂

  12. hello,

    The get_posts() function, in fact, can be customized to get products with a given stock, certain price range, same custom field value, search term, and so on.

    Thank you, but can you give another example ? Let’s say related products with the same price ?

    Your snippets are the best of the whole web ! thank you again !

    1. Hello there, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published. Required fields are marked *