在 WooCommerce 中有运费时如何隐藏免费送货 [英] How to hide free shipping when there is shipping costs in WooCommerce

查看:30
本文介绍了在 WooCommerce 中有运费时如何隐藏免费送货的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算佐治亚州不同城市的运费.

I am trying to make shipping costs with different cites in Georgia country.

我找到了这个代码:

function ace_change_city_to_dropdown( $fields ) {
    $cities = array( 
        'Tbilisi',
        'city2',
        // etc …
    );

    $city_args = wp_parse_args( array(
        'type' => 'select',
        'options' => array_combine( $cities, $cities ),
    ), $fields['shipping']['shipping_city'] );

    $fields['shipping']['shipping_city'] = $city_args;
    $fields['billing']['billing_city'] = $city_args;
    return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'ace_change_city_to_dropdown' );

然后我发现了 IwAR1w5zlVYKn2IcFgRc65w4gNEP9qgw8SzyKb1RDL27oFaLjIz2ng7A3OvWElow commerce%refern>IwAR1w5zlVYKn2IcFgRc65w4gNEP9qgw8SzyKb1RDL27oFaLjIz2ng7A3OvWElowernrefern可以用我想要的城市更改城市名称.它工作正常.

And I found woocommerce city rate shipping Where I can change city names with my desired cities. it's working fine.

但是如果有运费(例如 5 美元)我想隐藏免费送货复选框.

But when there shipping costs (for example 5$) I want to hide free shipping checkbox.

在 WooCommerce 中有运费时如何隐藏免费送货?

How to hide free shipping when there shipping costs in WooCommerce?

推荐答案

尝试以下操作,如果有任何有成本的送货方式可用,将隐藏免费送货:

Try the following, that will hide Free shipping if any shipping method with a cost is available:

add_filter( 'woocommerce_package_rates', 'hide_free_shipping_for_available_rate_costs', 100, 2 );
function hide_free_shipping_for_available_rate_costs( $rates, $package ) {
    $has_cost = false;
    
    foreach ( $rates as $rate_key => $rate ) {
        if( $rate-cost > 0 ) {
            $has_cost = true;
        } 
        if ( 'free_shipping' === $rate->method_id ) {
            $free_rate_key = $rate_key;
        }
    }
    
    if ( $has_cost && isset($free_rate_key) ){
        unset($rates[$free_rate_key]);
    }
    return $rates;
}

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

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

刷新运输缓存:

  1. 此代码已保存在您的 function.php 文件中.
  2. 在送货区域设置中,禁用/保存任何送货方式,然后启用返回/保存.

    大功告成,您可以对其进行测试.
  1. This code is already saved on your function.php file.
  2. In a shipping zone settings, disable / save any shipping method, then enable back / save.

    You are done and you can test it.

相关:WooCommerce - 隐藏免费送货时的其他送货方式

这篇关于在 WooCommerce 中有运费时如何隐藏免费送货的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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