2

In the magento2 checkout page, I'm using a free shipping method and I need to hide all shipping methods only for specific country and postal code. Kindly share your custom module or help me with this. Thanks

1 Answer 1

3

Create di.xml in Vendor/Module/etc folder,

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Api\ShipmentEstimationInterface">
        <plugin name="specific_shipping_method" type="Vendor\Module\Quote\Api\ShipmentEstimationPlugin"/>
    </type>
</config>

Create ShipmentEstimationPlugin.php in Vendor/Module/Plugin/Quote/Api folder,

<?php

namespace Vendor\Module\Plugin\Quote\Api;

use Magento\Quote\Api\ShipmentEstimationInterface;

class ShipmentEstimationPlugin
{
    public function __construct(
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
    ) {
        $this->customerSession = $customerSession;
        $this->customerRepository = $customerRepository;
    }

    public function aroundEstimateByExtendedAddress(
        ShipmentEstimationInterface $subject,
        \Closure $proceed,
        $cartId,
        \Magento\Quote\Api\Data\AddressInterface $address
    ) {
        $shippingMethods = $proceed($cartId, $address);
        if ($this->customerSession->isLoggedIn()) {
            $customerId = $this->customerSession->getCustomer()->getId();
            $customer = $this->customerRepository->getById($customerId);
            if ($address->getCountry() == "AF" && $address->getPostalCode() == "123456") {
                foreach ($shippingMethods as $key => $shippingMethod) {
                    unset($shippingMethods[$key]);
                }
            }

            return $shippingMethods;
        }
    }
}

Clear cache: php bin/magento cache:clean and check it.

NOTE: Set condition as per your requirements.

1

Your Answer

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

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