如果产品具有特定的运输类别,则向 WooCommerce 购物车项目添加自定义文本

当产品具有特定的运输类别时,我试图在结帐/购物车页面上的产品标题下方显示特定文本。我怎么做?

我很难在产品循环中添加文本。


子衿沉夜
浏览 133回答 1
1回答

慕尼黑5688855

以下将在购物车和结帐页面中的购物车项目名称下显示自定义文本,用于定义的运输类别:// Display a custom text under cart item name in cart pageadd_filter( 'woocommerce_cart_item_name', 'custom_text_cart_item_name', 10, 3 );function custom_text_cart_item_name( $item_name, $cart_item, $cart_item_key ) {&nbsp; &nbsp; // Here below define your shipping class slug&nbsp; &nbsp; $shipping_class = 'extra';&nbsp; &nbsp; if( is_cart() && $cart_item['data']->get_shipping_class() === $shipping_class ) {&nbsp; &nbsp; &nbsp; &nbsp; $item_name .= '<br /><div class="item-shipping-class">' . __("This is my custom text", "woocommerce") . '</div>';&nbsp; &nbsp; }&nbsp; &nbsp; return $item_name;}// Display a custom text under cart item name in checkoutadd_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_checkout_text_cart_item_name', 10, 3 );function custom_checkout_text_cart_item_name( $item_qty, $cart_item, $cart_item_key ) {&nbsp; &nbsp; // Here below define your shipping class slug&nbsp; &nbsp; $shipping_class = 'extra';&nbsp; &nbsp; if( $cart_item['data']->get_shipping_class() === $shipping_class ) {&nbsp; &nbsp; &nbsp; &nbsp; $item_qty .= '<br /><div class="item-shipping-class">' . __("This is my custom text", "woocommerce") . '</div>';&nbsp; &nbsp; }&nbsp; &nbsp; return $item_qty;}代码位于活动子主题(或活动主题)的 functions.php 文件中。测试和工作。
打开App,查看更多内容
随时随地看视频慕课网APP