1

I have developed custom shipping module. Now I want to show error message on shipping module if anything wrong like city, state, phone number etc.

I follow this link to create shipping method: https://www.mageplaza.com/magento-2-create-shipping-method/

How do I achieve this?

0

Override checkAvailableShipCountries() at

app/code/Vendor/Module/Model/Carrier/Myshipping.php

Here you can get city, state, country etc from \Magento\Framework\DataObject $request

Use $this->_rateErrorFactory for display error

Final Code:

public function checkAvailableShipCountries(\Magento\Framework\DataObject $request)
{
    $allowedCity = "ahmedabad,surat,morbi";
    $allowedCity = explode(',', $allowedCity);

    if(in_array($request->getDestCity(), $allowedCity)) {
        return $this;
    } else {
        $error = $this->_rateErrorFactory->create();
        $error->setCarrier($this->_code);
        $error->setCarrierTitle($this->getConfigData('title'));
        $errorMsg = $this->getConfigData('specificerrmsg');
        $error->setErrorMessage(__(
                'Sorry, but we can\'t deliver to the destination city with this shipping module.'
            )
        );
        return $error;
    }

    return $this;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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