0
我需要檢查特定產品是否屬於某個類別?從product_id獲取產品類別Woocommerce
public function check_add_to_cart_product() {
//Check to see if user has product in cart
$targetcatslug = array('indica', 'sativa', 'hybrid');//target categories slug
$incat=false;
$terms = get_the_terms($product_id,'product_cat');
if ($terms && ! is_wp_error($terms)) :
foreach ($terms as $term) {
$product_cat_slug= $term->slug;
if($product_cat_slug == $targetcatslug) {
$incat = true;
}
}
endif;
return $incat;
}
例如if $product_id = 456
,我想找到它的類別,並檢查它們是否屬於(「秈稻」,「sative」,「混合」)的類別。如果是,則返回true,否則返回false。我做錯了什麼?