I working in view.phtml and my project is can add in the cart two or more products in only one click. I explain my project...

In the product details we have a normal product and the button add to cart In the same page i would recall another simple/virtual product with a variable. So i would that when the customer push on "add to cart" in checkout there will be two product added simultaneously.

How can I do? What files can I touch?


  • Have you checked out how did the accessories products works ?
    – Giuseppe
    Feb 16, 2016 at 9:31
  • It's not a duplicate because i would do this operation inside magento and not outside. Not exactly, where to look? In the abstract.php?
    – Gabriel91
    Feb 16, 2016 at 9:48

1 Answer 1


Simplest way to achieve this might be to just add a hidden input to your product/view.phtml (inside the #product-addtocart-form), like

<input type="hidden" name="related_product" value="17,133,87"/>

... where 17,133,87 is a comma-separated list of the product ID's to add.

Basically, looking at the code in CartController::addAction(), it all comes down to using the Mage::getSingleton('checkout/cart')->addProductsByIds($arrayofproductids) method you could of course apply almost anywhere in code

+++ edited for improved flexibility +++

However if you need to apply any further logic on which products to add additionally or want to change the quantities you will have to set up a custom module with an event observer listening to checkout_cart_product_add_after event. You will find loads of hints how to do this on google.

In your event observer method you can best use

$product = Mage::getModel('catalog/product')->load($productid); Mage::getSingleton('checkout/session')->getQuote()->addProduct($product, $qty);

hope this helps ;)

  • Man, you're great!!! Thank you!! :D One more questione please. If I want to add three differen inputs with a single different value how can i do? Ah, sorry, one more question. When I add to cart two products at the same time does not show me the same quantity. How do I connect the same quantity for both of the products?
    – Gabriel91
    Feb 16, 2016 at 11:22
  • For the first question...because when i try to add to cart with two input of related product with two different value, cart just adds only one related product and not two...why? Thanks again.
    – Gabriel91
    Feb 16, 2016 at 12:08
  • if you use inputs like this '<input name="myinputname" value="1" /><input name="myinputname" value="2"/>' the second "myinputname" input value will overwrite the first one in $_POST. You should use input names like array indexes. Feb 16, 2016 at 12:32
  • Another drawback for your scenario is that the CartController::addAction() only accepts comma-separated values and not array-formed. so unfortunately you can't set up your input fields like <input type="hidden" name="related_product[]" value="17"/><input type="hidden" name="related_product[]" value="133"/>, which would form the $_POSTed data as array. Feb 16, 2016 at 12:38
  • Perfect man, you're very clear. Thanks again. One last question...for the quantity...if i would connect the same quantity of the simple product in product details with the related_product when push on addtocart, I have to create a custom module or there is a short way?
    – Gabriel91
    Feb 16, 2016 at 13:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.