WooCommerce - 当免费送货可用时隐藏其他送货方式 [英] WooCommerce - Hide other shipping methods when FREE SHIPPING is available

查看:35
本文介绍了WooCommerce - 当免费送货可用时隐藏其他送货方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 Woocommerce 提供免费送货服务时,我想隐藏其他送货选项.

因为即使有免费送货选项,最新版本的 woocommerce 现在仍然显示其他送货选项.

请帮忙

解决方案

有这个最近的 WooCommerce 2.6+ 代码片段.你可以使用:

add_filter( 'woocommerce_package_rates', 'hide_other_shipping_when_free_is_available', 100, 2 );函数 hide_other_shipping_when_free_is_available( $rates, $package ) {$free = 数组();foreach ( $rates as $rate_id => $rate ) {如果('free_shipping' === $rate->method_id){$free[ $rate_id ] = $rate;休息;}}返回 !空($free)?$free : $rates;}

<块引用>

您需要刷新运输缓存数据:在woocommerce运输设置中禁用、保存和启用、保存当前运输区域的相关运输方式.


对于 WooCommerce 2.5,你应该试试这个:

add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );函数 hide_shipping_when_free_is_available( $rates, $package ) {//仅在存在 free_shipping 时修改费率如果 ( isset( $rates['free_shipping'] ) ) {//要取消设置单个速率/方法,请执行以下操作.此示例取消设置 flat_rate 运费未设置( $rates['flat_rate'] );//要取消设置除 free_shipping 之外的所有方法,请执行以下操作$free_shipping = $rates['free_shipping'];$rates = array();$rates['free_shipping'] = $free_shipping;}返回 $rates;}

将此代码粘贴到位于活动子主题或主题的 function.php 文件中.


参考:免费送货时隐藏其他送货方式(官方文档)

相关:

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

解决方案

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

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: Hide other shipping methods when FREE SHIPPING is available (official doc)

Related:

这篇关于WooCommerce - 当免费送货可用时隐藏其他送货方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆