When a user try to add a new product to cart it is working fine. The number of product in cart is getting updated as one. But when he try to add another product to cart, then it is not getting added as a second product.The quantity of the first product is getting incremented. So the cart count is still one.This is happening when he tries to add multiple products. Only the quantity of the first product added to cart is getting updated no new product is being added.This is for logedin users only. I am using custom ajax call to add product to cart.


namespace V\Catalog\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Controller\Result\JsonFactory;

 * Class DellPost
 * @package Magebay\Hello\Controller\Index
class AddToCart extends \Magento\Framework\App\Action\Action
     * Result page factory
     * @var \Magento\Framework\Controller\Result\JsonFactory;
    protected $_resultJsonFactory;
    protected $formKey;
    protected $cart;
    protected $product;
     * @param Context $context
     * @param PageFactory $resultPageFactory
    public function __construct(
        Context $context,
        JsonFactory $resultJsonFactory,
        \Magento\Framework\Data\Form\FormKey $formKey,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Catalog\Model\Product $product
        $this->_resultJsonFactory = $resultJsonFactory;
        $this->formKey = $formKey;
        $this->cart = $cart;
        $this->product = $product;
    public function execute()
        $resultJson = $this->_resultJsonFactory->create();
        $response = [
            'message'=>'Error, Please try again'
        $params = $this->getRequest()->getParams();
        $productId = isset($params['product']) ? (int)$params['product'] : 0;
        if($productId > 0)
            try {
                $params['form_key'] = $this->formKey->getFormKey();
                $_product = $this->product->load($productId);
                $this->cart->addProduct($_product, $params);
                $totalItems = $cart->getQuote()->getItemsCount();
                $response = [
                    'message'=>'Product added to cart',
                    'total' => $totalItems
            catch(\Exception $e) {
                $response['message'] = $e->getMessage();

        return $resultJson->setData($response);

Please help

  • Could you add more details about your issue? Apr 19, 2020 at 14:18
  • I just updated my question. Can you check now.
    – skm
    Apr 19, 2020 at 14:23
  • Are you using a third-party modules that could potentially affect the "Add Product to Cart" process? Apr 19, 2020 at 14:35
  • Ya I am using a extension that adds free gift to cart, But I disabled it and no change. There is no other extension related to Add to Cart. I wonder why this issue not with guest users.
    – skm
    Apr 19, 2020 at 15:08
  • Updated question with code
    – skm
    Apr 19, 2020 at 21:45


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.