我想确定某个产品是否具有某个属性。例如:
if (product has attribute 'pa_color')
{
//do something
}
我该如何做到这一点?
答案 0 :(得分:3)
您只需以这种方式使用WC_Product
方法get_attribute()
:
// (If needed) Get an instance of the WC_Product Object from the product ID
$product = wc_get_product( $product_id );
// Get the product attribute value(s)
$color = $product->get_attribute('pa_color');
// if product has attribute 'pa_color' value(s)
if( ! empty( $color ) ){
// do something
} else {
// No product attribute is set for this product
}