Waseem
Waseem

Reputation: 193

How to create an Add to Cart button to add multiple products

I need to create an "add to cart" button in my upsell.phtml file to add two or more products to the cart.

I have the id of those products and now i had to add them to the cart with a single "add to cart" button, quantity is always be 1.

Thanks in advance.

Upvotes: 1

Views: 1804

Answers (2)

Pradeep Sanku
Pradeep Sanku

Reputation: 964

try to use this code

<?php $productids = $_POST['prod_ids']; ?>
<?php foreach($productids as $productid): ?>
    <?php $my_product = Mage::getModel('catalog/product')->load($productid); ?>
    <?php $cart = Mage::getModel('checkout/cart'); ?>
    <?php $cart->init(); ?>
    <?php $cart->addProduct($my_product, array('qty' => 1)); ?>
    <?php $cart->save(); ?>
    <?php Mage::getSingleton('checkout/session')->setCartWasUpdated(true); ?>
<?php endforeach; ?>

Upvotes: 0

Falaque
Falaque

Reputation: 896

Do this:

  1. On "Add to cart" button call a javaScript function say "FnAddToCart()"
  2. From "FnAddToCart()" send a ajax request to server. Along with request send the ids to add.
  3. In the server persist the data to the cart. You can do this in database of your choice.

Upvotes: 1

Related Questions