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
 * @community     https://businessbloomer.com/club/
 */

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 custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]
  • WooCommerce: Show Number Of Products Sold @ Product Page
    WooCommerce database already stores the number of products sold for you. Therefore, you may want to show such number on the product page, close to the Add To Cart button. As we’ve seen in my book Ecommerce and Beyond, showing the number of sales for each product can increase your sales conversion rate. All you […]

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. Follow @rmelogli

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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. 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 the Business Bloomer Club to get quick WooCommerce support. Thank you!

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