Check for a product category in a hooked function for WooCommerce products
You can use has_term()
WordPress conditional function to check for a category like:
add_action('woocommerce_before_single_product','for_preorder'); function for_preorder() { global $product; $categories = array( 50 ); // Here define your categories term Ids, slugs or names if ( has_term( $categories, 'product_cat', $product->get_id() ) ) { echo '<p>' . __("DISPLAY something here") . '</p>'; }}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
if( has_term( 50, 'product_cat' ) ) { // do something if current product in the loop is in product category with ID 50}
Or, if you pass the product ID in the for_preorder()
function, you can do:
if( has_term( 50, 'product_tag', 971 ) ) { // do something if product with ID = 971 has tag with ID = 50}