Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5490

Restrict custom shipping methods in Magento2

$
0
0

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

<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

Happy Coding 🙂


Viewing all articles
Browse latest Browse all 5490

Trending Articles