I want to use a foreach loop to loop through all products in the cart and if it exists and the user adds it to the cart again, it should increase that item's total by 1 but I am not sure how to implement this. What I want to happen is that if a user clicks "add to cart" on an item it gets added to the cart with quantity of 1. If they click "add to cart" again for the same item, it should change to quantity of 2 for that item.
-
Need more detail , magento already this feature .– mohanCommented Apr 5, 2018 at 7:34
-
noo....its not....i am using 1.9.3.2 version.– XYZCommented Apr 5, 2018 at 7:47
-
while clicking add to cart directly add quote table? or using magento default function?– mohanCommented Apr 5, 2018 at 12:11
1 Answer
That's how add to cart button in product page already works. You can check this function: Mage_Sales_Model_Quote_Item::addQty()
. It always adds a previous quantity. If it was not added to the cart before, the previous quantity added will be 0. Default previous quantity 0 is set here: Mage_Sales_Model_Quote::addProductAdvanced()
.
The default add to cart controller action from where everything is started from the button click is: Mage_Checkout_CartController::addAction()
.
So, from wherever you want to add items to cart, you should use one of that functions. For example: $item->addQty(1);