How to disable related products in Woocommerce in only one product? – TMC Systems

TMC Systems

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.

Note:
WordPress Code Warning: This article features code changes, or snippets, that you can make in your active themes function.php file. If you are unfamiliar with this task, or want to brush up, we have an article on Managing code snippets in WooCommerce. The technique provided can be used for both WordPress and WooCommerce sites.

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?

  1. 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)
  2. Add the above code to the functions.php file of your WordPress theme or child theme:
  3. 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;

https://tmcsystems.us/product/code-to-remove-related-products-in-variable-products/

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?

If 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