【问题标题】:How to hide free shipping when there is shipping costs in WooCommerceWooCommerce中有运费时如何隐藏免费送货
【发布时间】:2020-11-05 20:50:48
【问题描述】:

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

我找到了这段代码:

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' );

我找到了woocommerce city rate shipping,我可以在其中用我想要的城市更改城市名称。它工作正常。

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

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

【问题讨论】:

    标签: woocommerce


    【解决方案1】:

    尝试以下方法,如果有任何收费的送货方式可用,这将隐藏免费送货:

    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 文件中。经过测试并且可以工作。

    刷新运输缓存:

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

      您已完成,您可以对其进行测试。

    相关:WooCommerce - Hide other shipping methods when FREE SHIPPING is available

    【讨论】:

      猜你喜欢
      • 2018-08-09
      • 2015-08-14
      • 1970-01-01
      • 2016-11-11
      • 2021-08-13
      • 2018-03-05
      • 2020-06-22
      • 1970-01-01
      相关资源
      最近更新 更多