php 基于WooCommerce购物车中的运输类别显示自定义文本

shyt4zoc  于 2023-01-12  发布在  PHP
关注(0)|答案(1)|浏览(142)

这是我有什么,它的工作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; }
nlejzf6q

nlejzf6q1#

对于具有不同消息的多个发运分类,您可以使用以下选项:

// 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文件中的活动子主题(或活动主题)。它应该工作。

相关问题