Magento 2 – Custom Shipping Method Based on City

checkoutmagento2shipping

I want to show/hide custom shipping method like "Delivery in 2 hours" radio button if customer fills particular city on checkout page. If he is from that city will show "2 hour delivery" option if not will hide it.
Can anybody help me in this?

Thanks in advance.

Best Answer

I found this M2 Shipping Module helped me manipulate the shipping methods to suit my needs. I built to exclude delivery to certain postcodes and also use a custom attribute to set the method's price.


update (in light of question)

I guess it needs to install properly first but based on your question you might look at modifying something like these suggestions from the documentation

addMethod('id_005', [
    'title'      => "France, Germany, Switzerland, Spain, Italy",
    'enabled'    => in_array($request->dest_country_id, ['FR', 'DE', 'CH', 'ES', 'IT']),
    'price'      => 10,
]);
addMethod('id_006', [
    'title'      => "Postcode starting with 25",
    'enabled'    => $request->dest_country_id == 'FR' && substr($request->dest_postcode, 0, 2) == '25',
    'price'      => 10,
]);
addMethod('id_007', [
    'title'      => "Regular expressions allowing postal codes beginning with 'PO' (case insensitive)",
    'enabled'    => $request->dest_country_id == 'GB' && preg_match('/^PO.*$/i', $request->dest_postcode),
    'price'      => 10,
]);
Related Topic