【问题标题】:How to check if product has a specific product attribute in Woocommerce如何在 Woocommerce 中检查产品是否具有特定的产品属性
【发布时间】:2018-09-17 15:15:06
【问题描述】:

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

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

我怎样才能做到这一点?

【问题讨论】:

  • @LoicTheAztec 嘿,对不起,我忘了把它标记为正确答案。您的回答对我有用,谢谢。

标签: php wordpress woocommerce product custom-taxonomy


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2020-02-24
    相关资源
    最近更新 更多