Restrict product add to cart in magento 2
-
04-03-2021 - |
Question
i am using magento 2.1.5, and i have two tax classes. and i want to add only one tax class product in cart at a time. if customer try to add another tax class product, it will throw the massage.
for this i tried by adding plugin.
public function beforeaddItem(\Magento\Quote\Model\Quote $subject,$item)
{
$productTax = array();
$storeId = $this->storeManager->getStore()->getId();
$items = $this->quote->getItems();
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/cartblock.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info(json_encode($items));
foreach($items as $_item) {
$writer = new \Zend\Log\Writer\Stream(BP . 'var/log/cartblock.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info(json_encode($_item->getData()));
$productList = $this->productRepository->getById($_item->getProductId(), false, $storeId, true);
$productTax[] = trim($productList->getTaxClassificationKey());
}
$productNew = $this->productRepository->getById($item->getProductId(), false, $storeId, true);
if(!empty($productTax) && !in_array(trim($productNew->getTaxClassificationKey()),$productTax)){
throw new \Magento\Framework\Exception\LocalizedException(__('You only order Same Tax Class product.'));
}
return [$item];
}
}
But Its not working. Please help.
Solution
You can try to update your plugin code as described below.
Assume you are using a custom module name "Company_MyModule".
STEPP 1)
Create di.xml under YOUR-MAGENTO-ROOT/app/code/Company/MyModule/etc/
File: YOUR-MAGENTO-ROOT/app/code/Company/MyModule/etc/di.xml
<?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\Checkout\Model\Cart">
<plugin name="addtocartRestriction"
type="Company\MyModule\Model\Plugin\Cart"/>
</type>
</config>
STEP 2)
Create Cart.php under YOUR-MAGENTO-ROOT/app/code/Company/MyModule/Model/Plugin
File: YOUR-MAGENTO-ROOT/app/code/Company/MyModule/Model/Plugin/Cart.php
<?php
namespace Company\MyModule\Model\Plugin;
class Cart
{
protected $productRepository;
protected $storeManager;
protected $checkoutSession;
protected $logger;
public function __construct( \Magento\Catalog\Api\ProductRepositoryInterface $productrepositoryInterface, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Session\SessionManagerInterface $checkoutSession, \Psr\Log\LoggerInterface $loggerInterface )
{
$this->productRepository = $productrepositoryInterface;
$this->storeManager = $storeManager;
$this->checkoutSession = $checkoutSession;
$this->logger = $loggerInterface;
}
public function beforeAddProduct( \Magento\Checkout\Model\Cart $subject, $productInfo, $requestInfo = null )
{
$productTax = array();
$storeId = $this->storeManager->getStore()->getId();
$items = $this->checkoutSession->getQuote()->getItems();
$this->logger->info( json_encode( $items ) );
foreach ( $items as $_item ) {
$this->logger->info( json_encode( $_item->getData() ) );
$productList = $this->productRepository->getById( $_item->getProductId(), false, $storeId, true );
$productTax[] = trim( $productList->getTaxClassificationKey() );
}
$productNew = $this->productRepository->getById( $item->getProductId(), false, $storeId, true );
if ( !empty( $productTax ) && !in_array( trim( $productNew->getTaxClassificationKey() ), $productTax ) ) {
throw new \Magento\Framework\Exception\LocalizedException(__('You only order Same Tax Class product.'));
return $this;
}
return array(
$productInfo,
$requestInfo
);
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange