I Would like to hide shipping methods and display a custom message on the checkout page & cart page based on product attribute flag & if the shipping address is outside of the USA.
I used the below plugin but it triggers only logged-in users unable to verify with guest users.
Could you please advise what exact plugin or method want to use?
Note: It should work guest users & registered customers
app/code/Vendor/ShippingRestriction/etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<type name="Magento\Quote\Model\ShippingMethodManagement">
<plugin name="disableshippingmethod" type="Vendor\ShippingRestriction\Plugin\ShippingMethodManagementPlugin"/>
</type>
</config>
app/code/Vendor/ShippingRestriction/Plugin/ShippingMethodManagementPlugin.php
<?php
namespace Vendor\ShippingRestriction\Plugin;
use Psr\Log\LoggerInterface;
use Exception;
use Magento\Quote\Model\QuoteFactory;
class ShippingMethodManagementPlugin
{
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var QuoteFactory
*/
protected $quoteFactory;
/**
* ShippingMethodManagementPlugin constructor.
* @param LoggerInterface $logger
* @param \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
* @param QuoteFactory $quoteFactory
*/
public function __construct(
LoggerInterface $logger,
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
\Magento\Quote\Model\QuoteFactory $quoteFactory
)
{
$this->addressRepository = $addressRepository;
$this->logger = $logger;
$this ->quoteFactory = $quoteFactory;
}
/**
* @param \Magento\Quote\Model\ShippingMethodManagement $subject
* @param $result
* @param $cartId
* @param $addressId
* @return mixed
*/
public function afterEstimateByAddressId(\Magento\Quote\Model\ShippingMethodManagement $subject, $result, $cartId, $addressId)
{
$quote = $this->quoteFactory->create()->load($cartId);
$items = $quote->getAllVisibleItems();
$productAttributeValues = array();
$shippWithinUsaFlag = ['1'];
foreach ($items as $item) {
$productAttributeValues[$item->getSku()] = $item->getProduct()->getData('ship_only_within_usa');
}
//Fetch current Country Id US/IN based on shipping address id
$countryIdFlag = $this->getCustomerAddress($addressId);
//Unset shipping methods based on product attribute & shipping address
$restrictionFlag = (count(array_intersect($productAttributeValues, $shippWithinUsaFlag))) ? '1' : '0';
if($countryIdFlag !== "US" && $restrictionFlag === '1'){
/** @var TYPE_NAME $result */
foreach ($result as $key => $shippingMethod) {
unset($result[$key]);
}
}
return $result;
}
/**
* @param $addressId
* @return null|string
*/
public function getCustomerAddress($addressId)
{
$countryId = null;
try {
$addressRepository = $this->addressRepository->getById($addressId);
$countryId=$addressRepository->getCountryId();
return $countryId;
} catch (Exception $exception) {
$this->logger->error("Something went wrong with customer address".$exception->getMessage());
}
return $countryId;
}
}