Hide Related Products from specific products singl

2019-05-23 05:45发布

Okay so I am trying to hide the 'Related Products' section on the product page, BUT only for a specific product not all products.

I have found the following documentation to hide the section for all product pages: https://docs.woocommerce.com/document/remove-related-posts-output/

But as I mentioned this is how to hide the 'Related Products' section on all product pages. But I am looking for a way to hide this section ONLY on a specific product page.

Does anyone have any idea on how this can be achieved. I would greatly appreciate any help.

1条回答
再贱就再见
2楼-- · 2019-05-23 06:19

Try the following that will remove related products from specific defined product ID(s) page(s):

add_action( 'woocommerce_after_single_product_summary', 'remove_related_products_conditionally', 1 );
function remove_related_products_conditionally(){
    global $product;

    // HERE Define your targeted product Id(s) (in the array)
    $targeted_products = array( 37 );

    if( in_array( $product->get_id(), $targeted_products ) )
        remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

查看更多
登录 后发表回答