问题
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