0

Is it possible to hide a particular shipping method based on postcode only in the admin area?

When I say admin area I mean the Sales order page in the admin section.

I have tried around a plug-in

<type name="Magento\Shipping\Model\Shipping">
<plugin disabled="false" name="Vendor_Extension_Model_Shipping" sortOrder="10"
        type="Vendor\Extension\Plugin\ApplyShipping"/>

aroundCollectCarrierRates

But this isn’t helping me to get it done for postcodes

I can grab the postcode by request but when I select the shipping methods the particular shipping method still shows

Please can someone guide me or help me?

I have used this as a starting guide but this hide it completely

https://meetanshi.com/blog/enable-disable-shipping-method-programmatically-in-magento-2/amp/

I have used this to grab postcode https://magento.stackexchange.com/a/301477

1 Answer 1

1

Please try this, I am checking the condition for the areacode also

<?php

namespace Vendor\Extension\Plugin;

class ApplyShipping
{
    public function __construct(
        \Magento\Framework\App\State $appState
    ) {
        $this->appState = $appState;
    }

    public function aroundCollectCarrierRates(
        \Magento\Shipping\Model\Shipping $subject,
        \Closure $proceed,
        $carrierCode,
        $request
    ) {
        // To disable the shipping method
        if ($this->appState->getAreaCode() == 'adminhtml' &&
            $request->getDestPostcode() == 'postal_code' &&
            $carrierCode == 'carrier_code') {
            return false;
        }
        // To enable the shipping method
        return $proceed($carrierCode, $request);
    }
}

In the Above code replace the postal_code and carrier_code according to you, for which you want to disable the shipping for that particular postal code.

1
  • this was perfect thank you so much
    – Tj1997
    Commented Mar 22, 2022 at 22:25

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.