【问题标题】:Set dynamically shipping method label based on cart item count in WooCommerce根据 WooCommerce 中的购物车项目计数动态设置运输方式标签
【发布时间】:2020-01-30 20:44:12
【问题描述】:

在我的 woocommerce 购物车中,运费仍由以下代码正确计算。但是现在没有更新运输标签。我找不到原因;可能是最近的 woocommerce 3.7 更新?感谢您的帮助!

/**
 * Add different price for shipping based on products quantity
 */
function mx_shop_shipping_price( $rates, $package ) {
    //Shipping Mode 1
    if ( WC()->cart->cart_contents_count < 4 ) {
        return array_filter($rates, function (WC_Shipping_Rate $rate) {
            return $rate->get_label() === 'Shipping Mode 1'; // Shipping Mode 1
        });
    }
    //Shipping Mode 2
    if ( WC()->cart->cart_contents_count < 13 ) {
        return array_filter($rates, function (WC_Shipping_Rate $rate) {
            return $rate->get_label() === 'Shipping Mode 2';
        });
    }
    //Shipping Mode 3
    return array_filter($rates, function (WC_Shipping_Rate $rate) {
        return $rate->get_label() === 'Shipping Mode 3';
    });     
}
add_filter( 'woocommerce_package_rates', 'mx_shop_shipping_price', 10, 2 );

/** End

Cart detail with Shipping label not updated. Note Order Total is correctly updated

【问题讨论】:

  • 请对答案提供一些反馈。

标签: php wordpress woocommerce cart shipping-method


【解决方案1】:

您使用的是比较运算符“===”而不是赋值运算符。这是您可以尝试的更新代码:

function mx_shop_shipping_price( $rates, $package ) {
    //Shipping Mode 1
    if ( WC()->cart->cart_contents_count < 4 ) {
        return array_filter($rates, function (WC_Shipping_Rate $rate) {
            return  $rates[$rate_key]->label = __( 'Shipping Mode 1', 'woocommerce' ); // Shipping Mode 1
        });
    }
    //Shipping Mode 2
    if ( WC()->cart->cart_contents_count < 13 ) {
        return array_filter($rates, function (WC_Shipping_Rate $rate) {
            return $rates[$rate_key]->label = __( 'Shipping Mode 2', 'woocommerce' );
        });
    }
    //Shipping Mode 3
    return array_filter($rates, function (WC_Shipping_Rate $rate) {
        return $rates[$rate_key]->label = __( 'Shipping Mode 3', 'woocommerce' );;
    });     
}
add_filter( 'woocommerce_package_rates', 'mx_shop_shipping_price', 10, 2 );

您应该需要刷新运输缓存:

1) 首先,此代码已保存在您的 function.php 文件中。

2) 在配送设置中,输入配送区域并禁用 运输方式和“保存”。

然后重新启用该运输方式并“保存”。你已经完成了。

您可以找到已经给出的此类要求的答案here

【讨论】:

  • 我的代码设置运费!比较运算符用于返回匹配标签“Shipping Mode n”的费率。在标签自动更新之前。
猜你喜欢
  • 1970-01-01
  • 2021-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多