3

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.

3 Answers 3

1

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,
]);
5
  • I tried this module, but I think this is not working fine it throwing error when I installed it. it would be great if you could share your module
    – Ali Fani
    May 8, 2017 at 10:59
  • see my updated answer. hope it helps put you on the right path.
    – karty
    May 9, 2017 at 8:48
  • I installed this module, but when I logged into my admin panel and went under stores-configuration-sales-shipping method the page become blank
    – Ali Fani
    May 10, 2017 at 9:36
  • It gives this error Fatal error: Class 'Owebia\AdvancedSettingCore\Block\System\Config\Form\Field\AbstractField' not found in /var/www/testproject/punamdhote/Magento2Site/app/code/Owebia/AdvancedShippingSetting/Block/System/Config/Form/Field/About.php on line 9
    – Ali Fani
    May 10, 2017 at 9:41
  • sorry mate, i cannot comment on errors like these. maybe try going back to Owebia direct or look through their github issues (open and closed)
    – karty
    May 13, 2017 at 9:25
0

You should edit corresponding shipping class and add city validation, something like this:

/**
 * @param RateRequest $request
 * @return bool|Result
 */
public function collectRates(RateRequest $request)
{
    if (!$this->getConfigFlag('active')) {
        return false;
    }

    if ($request->getDestCity() != 'Minsk') { // add your city validation here
        return false;
    }

    /** @var \Magento\Shipping\Model\Rate\Result $result */
    $result = $this->_rateResultFactory->create();

    /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
    $method = $this->_rateMethodFactory->create();

    $method->setCarrier('shipping');
    $method->setCarrierTitle($this->getConfigData('title'));

    $method->setMethod('shipping');
    $method->setMethodTitle($this->getConfigData('name'));

    $amount = $this->getConfigData('price');

    $method->setPrice($amount);
    $method->setCost($amount);

    $result->append($method);

    return $result;
}

Previous code works if you want to remove the whole carrier with all methods. If you want to remove just one method you should add a validation before calling of the append($method) method:

if ($method->getMethod() == 'your_method_code') { // validate method
    if ($request->getDestCity() == 'Minsk') { // validate city
        $result->append($method);
    }
} else {
    $result->append($method);
}
6
  • Thank you for your response, but I would like to ask that in $request how city will come? if user has not fill the city field on checkout form?
    – Ali Fani
    May 10, 2017 at 9:21
  • Also $request->getDestCity() always remain empty when I logged it into log file
    – Ali Fani
    May 10, 2017 at 9:23
  • @AliFani which shipping method you are using? May 10, 2017 at 13:03
  • I have created custom shipping method and gave title "2 hour delivery", I want it to show based on shipping address, in both guest customer and logged in customer
    – Ali Fani
    May 10, 2017 at 13:33
  • @AliFani Could you share your code? May 10, 2017 at 13:35
-1

Thank you everyone for your help, but finally I solved the issue, getting postal code from Ajax and finding city on the basis of the postal code and then manipulating shipping option.

Thanks and Cheers

Ali

1
  • hey @ali how you did this please post the solution.
    – sudo55
    Feb 11, 2021 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.