I’m using this plugin in order to prevent a product be added more than one time and it works perfectly but I’d like to add a feature: instead to stay on the product page once the product is added more than one time, I’d like to check if the backend option Stores->Configuration->Sales->Checkout->Shopping Cart->After Adding a Product Redirect to Shopping Cart
is enabled and redirect if it is.
di.xml
<type name="MagentoCheckoutModelCart">
<plugin disabled="false" name="Vendor_Module_Plugin_Magento_Checkout_Cart_BeforeAddToCart" type="VendorModulePluginMagentoCheckoutCartBeforeAddToCart"/>
</type>
vendor/module/Plugin/Magento/Checkout/Cart/BeforeAddToCart.php
<?php
declare(strict_types=1);
namespace VendorModulePluginMagentoCheckoutCart;
use MagentoCheckoutModelCart;
use MagentoCatalogModelProduct;
use MagentoCheckoutModelSessionProxy as SessionProxy;
use MagentoFrameworkMessageManagerInterface;
use MagentoConfigurableProductModelProductTypeConfigurable;
class BeforeAddToCart {
protected $messageManager;
protected $cartSession;
protected $configurableProduct;
public function __construct(
Configurable $configurableProduct,
ManagerInterface $messageManager,
SessionProxy $cartSession
) {
$this->messageManager = $messageManager;
$this->cartSession = $cartSession;
$this->configurableProduct = $configurableProduct;
}
public function beforeAddProduct(Cart $subject, $productInfo, $requestInfo=null)
{
$enableProductCartControl=true;
$product = null;
$parentProduct=null;
if ($productInfo instanceof Product) {
$product = $productInfo;
if (!$product->getId()) {
throw new MagentoFrameworkExceptionLocalizedException(
__("The product wasn't found. Verify the product and try again.")
);
}
}
if ($product)
{
if ($product->getTypeId()==='configurable')
{
if (isset($requestInfo('super_attribute')))
{
$parentProduct=$product;
$childProduct = $this->configurableProduct->getProductByAttributes($requestInfo('super_attribute') ,$product);
// change $product to child
$product=$childProduct;
}
}
if ($product->getTypeId()==='grouped')
{
if (isset($requestInfo('super_group')))
{
$parentProduct=$product;
//$childProduct = // get child from grouped...
// change $product to child
//$product=$childProduct;
}
}
if ($product && $enableProductCartControl)
{
// check for existence of product in cart...
//
if($this->cartSession->getQuote()->hasProductId($product->getId())){
throw new MagentoFrameworkExceptionLocalizedException(
__("This product is already in the cart. Testing, testing : ". $product->getSku())
);
}
}
}
return ($productInfo, $requestInfo);
}
/**
* Get request for product add to cart procedure
*
* @param MagentoFrameworkDataObject|int|array $requestInfo
* @return MagentoFrameworkDataObject
* @throws MagentoFrameworkExceptionLocalizedException
*/
private function _getProductRequest($requestInfo)
{
if ($requestInfo instanceof MagentoFrameworkDataObject) {
$request = $requestInfo;
} elseif (is_numeric($requestInfo)) {
$request = new MagentoFrameworkDataObject(('qty' => $requestInfo));
} elseif (is_array($requestInfo)) {
$request = new MagentoFrameworkDataObject($requestInfo);
} else {
throw new MagentoFrameworkExceptionLocalizedException(
__('We found an invalid request for adding product to quote.')
);
}
return $request;
}
}