简体   繁体   中英

WooCommerce Shipping methods- Conditional Hide(Based on Billing and Shipping countries)

Here is my query.

I have different shipping methods. Ex:

  1. Shipping Using Customer A/C [Please enter your Fedex/ DHL /Other Courier Name and Account Number in the “Courier Info” Box above.]

  2. Free Shipping in India

  3. International

Now all the above are showing for all countries.

When the Billing country is India and shipping country is India –> I want 2nd option. (Free Shipping in India)

When the Billing country and is not India and shipping country is any –> I want 1st and 3rd Options.

(Screenshot Link: https://ibb.co/v38z524 )

Can you guide/ share me any hooks for the same which I can use in functions file.

Much appreciated.

Regards, Shridhar K

here is what I tried and it's working.

add_filter( 'woocommerce_package_rates', 'shipping_mathod_based_on_country', 100, 2 ); function shipping_mathod_based_on_country( $rates, $package ) { $country = WC()->customer->get_billing_country();

$condition = $country == "IN";


    if ( $condition ){
        $targeted_rate_id = 'free_shipping:4';
        unset($rates[$targeted_rate_id]);
        unset($rates['table_rate_shipping_international-1-unit']);  //to unset international
        unset($rates['table_rate_shipping_international-2-units']);
        unset($rates['table_rate_shipping_international-3-5-units']);
        unset($rates['table_rate_shipping_international-6-10-units']);
        unset($rates['table_rate_shipping_international-11-20-units']);
        unset($rates['table_rate_shipping_international-20-units']);
        
    }
    else{
        
        unset($rates['free_shipping:16']); //to unset free

        
    }
//}
return ! empty( $free ) && ! $condition ? $free : $rates;

}

Reference Link: Conditionally hide shipping methods in WooCommerce

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM