且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WooCommerce:检查产品是否具有属性

更新时间:2023-11-28 09:54:04

以这种方式尝试一下,我留下了代码进行调试,以便您可以详细查看WC_Product_Attribute对象.之后,您可以安全地删除此

Give it a try this way, I left the code to debug, so that you can view the WC_Product_Attribute Object in detail. Afterwards you can safely remove this

function flag1() {
    global $product;

    // for debug purposes, place in comment tags or delete this code
    $product_attributes = $product->get_attributes();
    echo '<pre>', print_r($product_attributes, 1), '</pre>';

    // Get the product attribute value
    $sprog = $product->get_attribute('pa_sprog');

    // if product has attribute 'sprog' value(s)
    if( $sprog == "dansk" ) {
        echo '<div class="">Dansk!</div>';
    } else {
        echo '<div class="">Engelsk!</div>';
    }
}
add_action( 'woocommerce_before_add_to_cart_form', 'flag1' );