【问题标题】:Display custom texts based on shipping classes in WooCommerce cart根据 WooCommerce 购物车中的运输类别显示自定义文本
【发布时间】:2021-02-19 14:16:20
【问题描述】:

这是我所拥有的,它适用于 1 个运输类别,但希望为不同的运输类别提供单独的消息:

// Display a custom text under cart item name in cart page
add_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 ) {
// Here below define your shipping class slug
$shipping_class = '5-gallon';

if( is_cart() && $cart_item['data']->get_shipping_class() === $shipping_class ) {
    $item_name .= '<br /><div class="item-shipping-class">' . __("Please call confirm shipping rates", "woocommerce") . '</div>';
}
return $item_name; }

【问题讨论】:

    标签: php wordpress woocommerce cart shipping


    【解决方案1】:

    对于具有不同消息的多个运输类别,您可以使用以下内容:

    // Display a custom text under cart item name in cart page
    add_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 ) {
        // Only on cart page
        if( ! is_cart() ) 
            return $item_name;
    
        // Here below define your shipping classes slugs (one by line)
        $shipping_class_1 = '5-gallon';
        $shipping_class_2 = '10-gallon';
    
        $shipping_class   = $cart_item['data']->get_shipping_class();
    
        // First shipping class
        if( $shipping_class === $shipping_class_1 ) {
            $item_name .= '<br /><div class="item-shipping-class">' . __("Please call confirm shipping rates", "woocommerce") . '</div>';
        }
        // 2nd shipping class
        elseif( $shipping_class === $shipping_class_2 ) {
            $item_name .= '<br /><div class="item-shipping-class">' . __("Some different message…", "woocommerce") . '</div>';
        }
    
        return $item_name; 
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 2019-09-18
      • 1970-01-01
      • 2015-12-10
      相关资源
      最近更新 更多