【问题标题】:How to show specific shipping options如何显示特定的运输选项
【发布时间】:2023-02-04 10:16:24
【问题描述】:

如何在结帐页面上向我的用户显示特定的送货选项?

我知道如何删除运输选项,但我无法在 $rates 中添加新的(现有的)。

我试图添加:

array_push($rates, 'flat_rate:5');

array_push 不应该做这项工作吗?

这是来自我的函数文件的基本 sn-p。

add_filter( 'woocommerce_package_rates', 'custom_package_rates', 10, 2 );
function custom_package_rates( $rates, $package ) {

    $total = WC()->cart->cart_contents_total;

    if( $total < 100 ) {

      // remove from shipping options
      unset( $rates['advanced_free_shipping'] );

      // Tryed it but critical error is thrown
      array_push($rates, 'flat_rate:5');
    }

    return $rates;
} 

尝试了我在堆栈和其他地方找到的每一段代码,似乎我是唯一有问题的人......

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce


    【解决方案1】:

    $rates 不是一个简单的数组,它还包含运输对象,因此添加一个数组 incl。运输对象是您要避免的东西。但你不需要。相反,您可以这样做:

    ...
    if( $total <= 100 ) {
      // remove from shipping options
      unset( $rates['advanced_free_shipping'] );
    }
    if( $total > 100 ) {
      // remove from shipping options
      unset( $rates['flat_rate:5'] );
    }
    ...
    

    它基本上做同样的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 1970-01-01
      • 2022-11-16
      • 1970-01-01
      相关资源
      最近更新 更多