2
votes

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

I found this code:

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

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

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

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

1

1 Answers

1
votes

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;
}

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

Refresh the shipping caches:

  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.

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