仅在特定产品变体属性中显示Woocommerce购物车中的消息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了仅在特定产品变体属性中显示Woocommerce购物车中的消息相关的知识,希望对你有一定的参考价值。

我正在向“购物车”页面添加自定义消息,但只有在添加的产品选择了自定义变体属性时才会显示。

经过研究,我走到了:

add_action( 'woocommerce_after_cart', 'wnd_after_cart' ); 
function wnd_after_cart() { 
    if($attribute_slug == 'no_review'){ 
        echo '<div class="wnd_after_cart"><h4>There will be no item manual review</h4><br /> </div>';
    }
}

但它不起作用。有谁知道我做错了什么?

add_action( 'woocommerce_after_cart', 'wnd_after_cart' ); 
function wnd_after_cart() { 
    echo '<div class="wnd_after_cart"><h4>There will be no item manual review</h4><br /> </div>'; 
}

工作得很好,但只有在我的自定义属性被选中时,我才能获得显示消息的代码。

任何帮助表示赞赏......

编辑:

我设置了我的产品Atributes(例如:衬衫尺寸,Slug:'Shirt_Size'),以及它们在这个属性中的变化(例如:S(Slug:'Size_S'),M(Slug:'Size_M'),XL(Slug:' Size_XL'))

我正在尝试在选择特定的属性变化时显示消息(例如:S的slug,'Size_S')

我正在使用衣服/衬衫尺码,因为它是一个更常见的例子来帮助说明。

如果我没有很好地解释,基本上代码是搜索属性slug,你可以在这个视频0:23在这里看到https://www.youtube.com/watch?v=QyMuq-WkV0o

但我试图让它搜索可以在0:35看到的属性变化的slu((属性属性,或属性变体,或属性子,我不知道该命名为什么)

@LoicTheAztec代码似乎运行得很好,但它正在搜索属性的slug(Shirt_Size),而不是搜索显示的属性变化。当我设置代码以找到'Shirt_size'时,它将显示消息,但当我将其设置为'Size_S'时,它将停止工作。

这有意义吗?再次感谢您的关注和建议。

答案

有一些丢失的代码,比如从购物车项目的变化中获取$attribute_slug

add_action( 'woocommerce_after_cart', 'checking_variation_attributes_message' );
function checking_variation_attributes_message() {
    $found = false;
    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ){
        $product = $cart_item['data'];
        if( ! $product->is_type('variation')){
            continue; // Jump to next cart item
        }
        $variation_attributes = $product->get_variation_attributes();
        foreach ( $variation_attributes as $variation_attribute => $term_slug ){
            $attribute_slug = str_replace('attribute_pa_', '', $variation_attribute);

            if( $attribute_slug == 'no_review' ){
                $found = true;
                break;
            }
        }
    }
    if($found){
        echo '<div class="wnd_after_cart"><h4>There will be no item manual review</h4><br /> </div>';
    }
}

更新:或者如果您正在寻找产品属性术语slug,请使用以下方式:

add_action( 'woocommerce_after_cart', 'checking_variation_attributes_message' );
function checking_variation_attributes_message() {
    $found = false;
    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ){
        $product = $cart_item['data'];
        if( ! $product->is_type('variation')){
            continue; // Jump to next cart item
        }
        $variation_attributes = $product->get_variation_attributes();
        foreach ( $variation_attributes as $variation_attribute => $term_slug ){
            $attribute_slug = str_replace('attribute_pa_', '', $variation_attribute);

            if( $term_slug == 'no_review' ){
                $found = true;
                break;
            }
        }
    }
    if($found){
        echo '<div class="wnd_after_cart"><h4>There will be no item manual review</h4><br /> </div>';
    }
}

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

以上是关于仅在特定产品变体属性中显示Woocommerce购物车中的消息的主要内容,如果未能解决你的问题,请参考以下文章

从所选变体向 WooCommerce 变量产品标题添加一些属性值

WooCommerce 中特定产品变体的自定义后缀

以编程方式创建具有新属性值的 WooCommerce 产品变体

在 WooCommerce 产品循环中显示特定类别的产品属性值

WooCommerce 如何在前端获取产品属性的干净标签

WooCommerce:还在购物车项目上显示产品变体描述