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
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.
-
Comments are not for extended discussion; this conversation has been moved to chat. Jan 3, 2020 at 13:09