为了账号安全,请及时绑定邮箱和手机立即绑定

WooCommerce - 根据购物车小计隐藏/显示运输方式

WooCommerce - 根据购物车小计隐藏/显示运输方式

PHP
慕神8447489 2023-07-21 16:06:01
在我的 WooCommerce 商店(使用版本 4.2.2)中,我想隐藏/显示一些基于购物车小计的运输方式,如下所示:低于 25 欧元:仅显示运输方式 A 和 B,25 至 49 欧元之间:仅显示运输方式 C 和 D,50 欧元或以上:仅显示免运费请注意,运输方式 A、B、C 和 D 均为“统一费率”。我用谷歌搜索了这个并设法通过以下代码尝试得到这个(我只是用一种速率和一个阈值进行测试):add_filter( 'woocommerce_package_rates', 'hide_shipping', 10, 2 );function hide_shipping( $rates, $package ) {    // Retrieve cart subtotal    global $woocommerce;    $cart_subtotal = $woocommerce->cart->get_subtotal();     if( $cart_subtotal > 25 ){        unset( $rates['flat_rate:7'] );    }     return $rates;}但代码没有任何效果。我哪里错了?
查看完整描述

1 回答

?
回首忆惘然

TA贡献1847条经验 获得超11个赞

请尝试以下操作(在开头的代码中设置您的 5 种运输方式费率 ID)。另外,对于“免费送货”费率,请将“最低订单金额”设置为(零)。0


add_filter( 'woocommerce_package_rates', 'hide_specific_shipping_method', 10, 2 );

function hide_specific_shipping_method( $rates, $package ) {

    // Settings: define you shipping rate IDs below

    $rate_id_1     = 'flat_rate:7';

    $rate_id_2     = 'flat_rate:11';

    $rate_id_3     = 'flat_rate:12';

    $rate_id_4     = 'flat_rate:15';

    $rate_free     = 'free_shipping:5';

    

    $cart_subtotal = WC()->cart->get_subtotal();

    

    if ( $cart_subtotal < 25 ) {

        // Enable only methods 1 et 2

        if ( isset($rates[$rate_id_3]) )

             unset( $rates[$rate_id_3] );

        if ( isset($rates[$rate_id_4]) )

             unset( $rates[$rate_id_4] );

        if ( isset($rates[$rate_free]) )

             unset( $rates[$rate_free] );

    } 

    elseif ( $cart_subtotal >= 25 && $cart_subtotal < 50 ) {

        // Enable only methods 3 et 4

        if ( isset($rates[$rate_id_1]) )

             unset( $rates[$rate_id_1] );

        if ( isset($rates[$rate_id_2]) )

             unset( $rates[$rate_id_2] );

        if ( isset($rates[$rate_free]) )

             unset( $rates[$rate_free] );

    } 

    else {

        // Enable only Free shipping

        if ( isset($rates[$rate_id_1]) )

             unset( $rates[$rate_id_1] );

        if ( isset($rates[$rate_id_2]) )

             unset( $rates[$rate_id_2] );

        if ( isset($rates[$rate_id_3]) )

             unset( $rates[$rate_id_3] );

        if ( isset($rates[$rate_id_4]) )

             unset( $rates[$rate_id_4] );

    }

    return $rates;

}

代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。

重要提示:刷新运输缓存:
1)。此代码已保存在您的 function.php 文件中。
2)。在运输区域设置中,禁用/保存任何运输方式,然后启用返回/保存。
你已经完成了,你可以测试它。


查看完整回答
反对 回复 2023-07-21
  • 1 回答
  • 0 关注
  • 98 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信