Target: I want to add different shipping cost for the different payment gateways depending on the pin code user have.
Tried Using Shipping Zones:
I have 3 different shipping zones added in my woo-commerce dashboard. All the zones are created using the pin codes of the city and flat rate. However, if the pin code exists in multiple zones, then the top level zone gets the first priority and all other zones get ignored at the checkout page.
Now I want to change that zone depending on the Payment Gateway user chooses. For example user, 'A' has the pin code '123456' and this pin code exists in two Shipping Zones: 'SH1', 'SH2'. So, if the user chose 'Cash On Delivery' option, then I want to disable shipping zone 'SH1' for him while activating 'SH2'.
Here is the code I tried, but not works.
add_filter( 'woocommerce_available_payment_gateways', 'change_user_zone', 20, 1);
function change_user_zone( $gateways ){
$targeted_zones_names = array('COD FCity'); // Targeted zone names
$current_payment_gateway = WC()->session->get('chosen_payment_method'); // Selected payment gateway
// Current zone name
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping mehod
$chosen_method = explode(':', reset($chosen_methods) );
$shipping_zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $chosen_method[1] );
$current_zone_name = $shipping_zone->get_zone_name();
if($current_payment_gateway === "cod" && $current_zone_name === "COD FCity"){
$WC_Shipping_Zone = new WC_Shipping_Zone();
$var = $WC_Shipping_Zone->set_zone_locations( "India - Maharashtra - Mumbai" );
}
return $gateways;
}
Please help!
Note: I'm looking for a coding solution. If you want to suggest any plugin for this, then use the comment section, please.