【问题标题】:Append text to product title if product has product-tag on cart in WooCommerce如果产品在 WooCommerce 中的购物车上有产品标签,则将文本附加到产品标题
【发布时间】:2020-06-20 13:17:54
【问题描述】:

如果产品具有特定标签,我正在尝试将文本附加到购物车中的 WooCommerce 产品标题。

这就是我所拥有的。我错过了条件代码。

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

}

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

【问题讨论】:

    标签: php wordpress woocommerce product cart


    【解决方案1】:
    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 );
    

    【讨论】:

      猜你喜欢
      • 2014-11-21
      • 2015-03-31
      • 1970-01-01
      • 2014-02-06
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 2018-05-18
      • 1970-01-01
      相关资源
      最近更新 更多