【问题标题】:WooCommerce: check if product has attributeWooCommerce:检查产品是否具有属性
【发布时间】:2020-06-03 00:51:44
【问题描述】:

我正在尝试将文本附加到包含属性“pa_sprog”值“dansk”的产品

我在this 发帖工作 这是我目前所拥有的:

add_action( 'woocommerce_before_add_to_cart_form', 'flag1' );    
function flag1() {
    $product = wc_get_product( $product_id );

    // 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 {
        // Engelsk
    }
}

【问题讨论】:

    标签: php wordpress woocommerce attributes hook-woocommerce


    【解决方案1】:

    这样试一试,我把代码留给调试了,你可以详细查看WC_Product_Attribute对象。之后您可以安全地删除它

    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' );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      相关资源
      最近更新 更多