dongmao3131 2016-07-09 10:01
浏览 64

WooCommerce - 免费送货时隐藏其他送货方式

I would like to hide other shipping options when free shipping is available on Woocommerce.

Because latest version of woocommerce now is still showing other shipping options even if there's FREE shipping option.

Please help

  • 写回答

1条回答 默认 最新

  • doumiang2297 2016-07-09 10:53
    关注

    There is this recent code snippet for WooCommerce 2.6+. that you can try:

    add_filter( 'woocommerce_package_rates', 'hide_other_shipping_when_free_is_available', 100, 2 );
    
    function hide_other_shipping_when_free_is_available( $rates, $package ) {
    
        $free = array();
        foreach ( $rates as $rate_id => $rate ) {
            if ( 'free_shipping' === $rate->method_id ) {
                $free[ $rate_id ] = $rate;
                break;
            }
        }
        return ! empty( $free ) ? $free : $rates;
    }
    

    You will need to refresh shipping cached data: disable, save and enable, save related shipping methods for the current shipping zone, in woocommerce shipping settings.


    For WooCommerce 2.5, You should try this:

    add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
    
    function hide_shipping_when_free_is_available( $rates, $package ) {
    
        // Only modify rates if free_shipping is present
        if ( isset( $rates['free_shipping'] ) ) {
    
            // To unset a single rate/method, do the following. This example unsets flat_rate shipping
            unset( $rates['flat_rate'] );
    
            // To unset all methods except for free_shipping, do the following
            $free_shipping          = $rates['free_shipping'];
            $rates                  = array();
            $rates['free_shipping'] = $free_shipping;
        }
    
        return $rates;
    }
    

    Paste this code in the function.php file located in your active child theme or theme.


    Reference:

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题