如何检查产品是否在 Woocommerce 中具有特定的产品属性

How to check if product has a specific product attribute in Woocommerce

我想确定产品是否具有属性。例如:

if (product has attribute 'pa_color')
{
    //do something
}

我怎样才能做到这一点?

您可以这样使用 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
}