Hide Related Products from specific products single pages in Woocommerce

馋奶兔 提交于 2020-01-02 15:51:48

问题


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:


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.



来源:https://stackoverflow.com/questions/51831339/hide-related-products-from-specific-products-single-pages-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!