MAGE SE Detect product in cart and redirect

https://magento.stackexchange.com/questions/331571/how-to-prevent-adding-same-product-to-cart-more-than-one-time/331618

Plugin to detect a cart condition, i.e. product already added and error or redirect...

 

        if ($product && $product->getSku()==='MJ01-XS-Yellow')
        {
            // default product is simple
            //
            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 && $enableProductCartControl)
                {
                    // check for existence of product in cart...
                    //
                    if($this->cartSession->getQuote()->hasProductId($product->getId())){

                        // redirect to cart
                        //$this->session->setRedirectUrl($this->url->getUrl('checkout/cart/index'));
                        throw new \Magento\Framework\Exception\LocalizedException(
                            __("[x] This product is already in the cart. Testing, testing : ". $product->getSku())
                        );
                    }

                }


        }
View as Grid List

1 Item

per page
Set Descending Direction
View as Grid List

1 Item

per page
Set Descending Direction
Copyright © 2013-present Magento, Inc. All rights reserved.