How to disable related products in Woocommerce in only one product?
Do you have this problem?
Need to hide related products in the Woocommerce product page so only the displayed product visible. Here we teach you how to do 2 things one is how to disable all related products across entire store and the other one is how to disable showing related products in only one product page while in other places it will show up.
How do I stop all related products from showing in the entire store?
Option 1: Using your theme
If you are using a pre-built theme, chances are that they have an option to control this section. It’s likely something you may find under the single product page options. If you’re theme has this customization option built in then you can remove the related products without using any code.
So be sure to check your theme settings or options to see if this is something you can just enable or disable there! If you’re unsure where to look check for your themes options page, if it provides one, or the WordPress theme cutomizer.
Option 2: Using some code
If you are using a custom built theme, or a theme that doesn’t have an option to remove the related products from the single product page – you’re not out of luck. You can add the following code snippet to your theme’s functions.php file. Take note, we provide two methods to accomplish this task – you should try the top method first and attempt the alternative method if that one does not work.
functions.php
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
functions.php (Alternative)
add_filter('woocommerce_product_related_posts_query', '__return_empty_array', 100);
This single line of code should be enough to disable the related products from showing up. In fact, this line actually simply removes the related products ‘action‘ from being run on the page all together. If for some reason this does not work for you and the theme you’re using you can try the second (alternative) method provided!
How do I stop related products from showing in one specific product?
Here’s how you can display a “checkbox” in the product edit page in order to hide the Related Products section in case this is checked. This is something you can also reuse to hide other sections in the same way – for example you might need to hide product tabs or featured image in certain cases.
PHP Snippet: Hide Related Products on a Per-Product Basis
/**
* @snippet Checkbox to hide Related Products - WooCommerce
* @author TMC Systems
* @compatible WooCommerce 3.5.7
* @check services tmcsystems.us
*/
// -----------------------------------------
// 1. Add new checkbox product edit page
add_action(
'woocommerce_product_options_general_product_data'
,
'bbloomer_add_related_checkbox_products'
);
function
bbloomer_add_related_checkbox_products() {
woocommerce_wp_checkbox(
array
(
'id'
=>
'hide_related'
,
'class'
=>
''
,
'label'
=>
'Hide Related Products'
)
);
}
// -----------------------------------------
// 2. Save checkbox into custom field
add_action(
'save_post_product'
,
'bbloomer_save_related_checkbox_products'
);
function
bbloomer_save_related_checkbox_products(
$product_id
) {
global
$pagenow
,
$typenow
;
if
(
'post.php'
!==
$pagenow
||
'product'
!==
$typenow
)
return
;
if
( defined(
'DOING_AUTOSAVE'
) && DOING_AUTOSAVE )
return
;
if
( isset(
$_POST
[
'hide_related'
] ) ) {
update_post_meta(
$product_id
,
'hide_related'
,
$_POST
[
'hide_related'
] );
}
else
delete_post_meta(
$product_id
,
'hide_related'
);
}
// -----------------------------------------
// 3. Hide related products @ single product page
add_action(
'woocommerce_after_single_product_summary'
,
'bbloomer_hide_related_checkbox_products'
, 1 );
function
bbloomer_hide_related_checkbox_products() {
global
$product
;
if
( !
empty
( get_post_meta(
$product
->get_id(),
'hide_related'
, true ) ) ) {
remove_action(
'woocommerce_after_single_product_summary'
,
'woocommerce_output_related_products'
, 20 );
}
}
Where to add this snippet?
- Go to Appearance tab->Theme Editor (Sometimes this is not there so in that case use a file manager plugin to go to wp-content->themes->find your theme name and go inside it(eg:-Astra)
- Add the above code to the functions.php file of your WordPress theme or child theme:
- It should be placed at the very bottom of the php file and make sure you have hosting access of your website incase you place it in wrong place and website crashes.
Do You Need To Remove Related Products In Variable Products As Well?
As you may have noticed the above code doesn’t work if your site has variable products but only works for simple products.
For this a more custom code is required which you may purchase here at a very affordable price by clicking on add to cart/checkout button. Once purchase completed you will receive the code on your email automatically to add to your website as per above.
This is the solution for all the people searching on search engine like google the following words;
How to disable related products in Woocommerce in only one product?
How To Remove Related Products In Variable Products Of Woocommerce?
If you are tired of trying to solve this yourself and want an easy way out you could hire us to develop or finish developing your website for you do checkout our services here.
If this helped you out you can support us by donating, well not exactly donating but purchasing a pro plugin which might be useful for you later for super cheap here
Updated Solution On 21st June 2022
Reason Behind Sharing WooCommerce Related Products Removal Code?
Every web developer or website creator believes in Woocommerce plugins usefulness but they can’t buy WooCommerce Coding Customizers because of the high price problems, so we have shared the most cost effective custom code for purchase to support start up’s, SMB’s & upcoming developers.
Click add to cart and get the code for really cheap for a limited time, proof of code working is in gallery as well. No refunds offered as files have been checked to be working already. By making a purchase you automatically agree to our T&C.
Important Points
Point 1 – Never Update the Woocommerce plugin from your WordPress Dashboard directly. It can break some features.
Pont 2 – If you’re facing any issues, first remove code from php file and check if ok after that, reinput code.
Please Note
Copyright Notices: This website is not sharing code or plugins by cloning or cracking. We respect the GNU General Public License (GPL) and the hard work of the theme/plugin creator. On this website, we provide the link of the related file which is already stored somewhere else privately on the internet and is not a part of this website.
Our website does not carry any responsibility for them. If your copyrighted material has been indexed by our site and you want this material to be removed then contact us immediately. We will remove it asap.
Thank you
This post suitable for those who search these words on google search engine example –
How to disable related products in Woocommerce in only one product, How To Remove Related Products In Variable Products Of Woocommerce, customize woocommerce related products, disable woocommerce related products, tmc systems, tmcsystemsus, tmcsystems.us, tmcsystems.
Reviews
There are no reviews yet.