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.
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;
}
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?
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!
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. ?
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!
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
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!
Hello! Do you have any idea of how I can customize the HTML output of the related products items?
Best regards and thanks!
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!
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.
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!
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.
Thanks – but do you get an actual error?
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!
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!
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
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!
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?
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!
Hi there,
I added your code to my functions.php file. But now nothing (no related products) are showing up..
Any advice?
Hi Steff! Did you customize my snippet or use the exact same?
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.
Hey Cor, sorry about that. I was off for 3 weeks plus it usually takes me 2 weeks to reply. I do my best ๐
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 !
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!