Reading list Switch to dark mode

    Restrict custom shipping methods in Magento 2

    Updated 22 February 2024

    Many times we want to restrict some shipping methods according to some conditions. So if you are looking for the same, we have a solution for you.

    We have to create an after plugin for collectRates. So here is our di.xml code. If you are not aware with shipping method development then please visit – how to create Magento 2 shipping method

    <type name="Magento\Shipping\Model\Shipping">
            <plugin name="Webkul_ShippingRestrict::rate" type="Webkul\CustomRestriction\Plugin\Model\Shipping"/>
        </type>

    Now, we will create plugin on the defined path i.e, Webkul\CustomRestriction\Plugin\Model with name Shipping.php

    <?php
    
    namespace Webkul\CustomRestriction\Plugin\Model;
    
    class Shipping
    {
        /**
         * @var \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory
         */
        private $rateErrorFactory;
    
        /**
         * @var \Magento\Quote\Model\Quote\Address\RateRequest|null
         */
        private $request = null;
    
        /**
         * construct
         *
         * @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
         * @param \Webkul\ShippingRestrictions\Model\RestrictionRule $restrictionRule
         * @param \Webkul\ShippingRestrictions\Helper\Data $helper
         */
        public function __construct(
            \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
        ) {
            $this->rateErrorFactory = $rateErrorFactory;
        }
    
        /**
         * before plugin
         *
         * @param \Magento\Shipping\Model\Shipping $subject
         * @param \Magento\Quote\Model\Quote\Address\RateRequest $request
         * @return void
         */
        public function beforeCollectRates(
            \Magento\Shipping\Model\Shipping $subject,
            \Magento\Quote\Model\Quote\Address\RateRequest $request
        ) {
            $this->request = $request;
        }
    
        /**
         * after plugin
         *
         * @param \Magento\Shipping\Model\Shipping $subject
         * @param \Magento\Shipping\Model\Rate\Result] $result
         * @return void
         */
        public function afterCollectRates(\Magento\Shipping\Model\Shipping $subject, $result)
        {
            $result = $subject->getResult();
            $rates = $result->getAllRates();
            $result->reset();
            foreach ($rates as $rate) {
                $restrict = false;
                if ($rate->getCarrier() == 'custom_shipping') {
                    $restrict = true;
                    $this->setError($result, $rate);
                }
                if (!$restrict) {
                    $result->append($rate);
                }
    
            }
    
            return $subject;
        }
    
        /**
         * set error message
         * @param \Magento\Shipping\Model\Rate\Result $result
         * @param \Magento\Quote\Model\Quote\Address\RateResult\Method $rate
         *
         * @return bool
         */
        private function setError($result, $rate)
        {
            $errorMessage = __('Sorry, shipping method is restricted');
    
            if ($rate !== null && $errorMessage) {
                $error = $this->rateErrorFactory->create();
                $error->setCarrier($rate->getCarrier());
                $error->setCarrierTitle($rate->getCarrierTitle().' ('.$rate->getMethodTitle().')');
                $error->setErrorMessage($errorMessage);
    
                $result->append($error);
            }
        }
    }

    That’s all we have to do.

    Thank you

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    Happy Coding 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home