如果产品在WooCommerce中的购物车上有产品标签,则在产品标题后附加文本 [英] Append text to product title if product has product-tag on cart in WooCommerce

查看:60
本文介绍了如果产品在WooCommerce中的购物车上有产品标签,则在产品标题后附加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果产品具有特定标签,我想在购物车中的WooCommerce产品标题后附加文本。

I'm trying to append text to WooCommerce product title in the cart if products has a specific tag.

这就是我所拥有的。我误解了条件代码。

This is what i have. I mis the conditional code.

add_filter( 'woocommerce_cart_item_name', 'add_udstilling_below_cart_item_name', 10, 3);
function add_udstilling_below_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
    echo $item_name . ' (test)';
}

我尝试过类似的操作:

if ( has_term( 'udstillingsmodel', 'product_tag' ) ) {

}

但是我需要帮助才能使其与购物车中的产品一起使用。

But i need help getting it to work with products in cart.

推荐答案

function add_udstilling_below_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
    $product_id = $cart_item['product_id'];

    /* Uncomment for debug purposes
    $terms = wp_get_post_terms( $product_id, 'product_tag' );
    echo '<pre>', print_r($terms, 1), '</pre>';
    */

    if ( has_term( 'udstillingsmodel', 'product_tag', $product_id ) ) {
        $item_name = $item_name . ' (test)';
    }

    return $item_name;
}
add_filter( 'woocommerce_cart_item_name', 'add_udstilling_below_cart_item_name', 10, 3 );

这篇关于如果产品在WooCommerce中的购物车上有产品标签,则在产品标题后附加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆