I have to add multiple items into magento cart from out side of magento (external website), through /path/to/app/checkout/cart/add?product=[id]&qty=[qty]
this method I can add one product to the cart.
Can anyone suggest how to do it?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI have to add multiple items into magento cart from out side of magento (external website), through /path/to/app/checkout/cart/add?product=[id]&qty=[qty]
this method I can add one product to the cart.
Can anyone suggest how to do it?
There is a way, but you cannot add quantities to the rest of the products. They will be added in minimum qty. (most probably 1).
/checkout/cart/add/?product=[id]&qty=[qty]&related_product=[id2],[id3],...,[idN]
It works even if products with ids id2, .., idN
are not actually related products to the main product.
Click this for demo:http://demo.magentocommerce.com/checkout/cart/add/?product=51&qty=2&related_product=17,18.
But be careful. In ce1.8 this doesn't work. Actually just adding a simple product to the cart doesn't work anymore (I mean this checkout/cart/add?product=[id]&qty=[qty]
). Starting with this version the form_key
is required when adding something to the cart.
Since this form_key
depends on the session you may want to create your own controller and action that receives the products to be added to the cart and redirects to checkout/cart/add?product=[id]&qty=[qty]&form_key=FORM_KEY_HERE
It might not be the easiest way and might be difficult to maintain on magento updates but you could add your own controller. Ideally one that extends the core Mage_Checkout_CartController
, but call yours from a new router. Then you could add your own function that uses the core functionality to add products but just takes in a customized format of parameters.
Config.xml
<routers>
<new_checkout>
<use>standard</use>
<args>
<module>New_Checkout</module>
<frontName>new-checkout</frontName>
</args>
</new_checkout>
</routers>
New_Checkout_CartController
<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class New_Checkout_CartController extends Mage_Checkout_CartController
{
public function addMultipleAction() {
$cart = $this->_getCart();
//Loop through each set of parameters for a products
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load($productId);
$cart->addProduct($product, $params);
$cart->save();
}
}
If you send the parameters in a way that each individual product is the same as the core cart expects than the addProduct function will work well.
Note There is no error checking in my basic example but I would recommend adding them. Look through the core controller to see how it works and mirror that when you need it.
Here is a solution, please check it out. Taken from http://deepakbhatta.com/magento-add-multiple-items-to-cart/
How to adds multiple items to shopping cart, at a time?
Normally this code is kept into controller.
<?php
class Packagename_Modulename_IndexController extends Mage_Core_Controller_Front_Action
{
public function multipleProdAddAction()
{
$items = array("1" => "sku1" , "2" => "sku2");
$invalidProduct = array();
$validProducts = array();
foreach($items as $qty=>$sku) {
$id = Mage::getModel('catalog/product')->getIdBySku($sku);
//Check whether the sku exists or not.
if(!$id) {
$invalidProduct[$sku] = $id;
}
else {
$validProducts[$sku] = $quantity;
}
}
//If any of the product we are adding, is not valid, we are not adding it to cart.
if(count($invalidProduct) < 1 && count($validProducts) > 0) {
try{
$cart = Mage::helper('checkout/cart')->getCart();
$ms="";
foreach($validProducts as $sku => $qty) {
$params = array('qty' => $qty);
$id = Mage::getModel('catalog/product')->getIdBySku($sku);
$product = Mage::getModel('catalog/product')->load($id);;
$cart->addProduct($product, $params);
$msg .= $product->getName(). " is successfully added into cart<br>";
}
$cart->save();
Mage::getSingleton('core/session')->addSuccess(Mage::helper('checkout')->__($msg));
$this->_redirect('checkout/cart');
}
catch(Exception $e) {
Mage::getSingleton('core/session')->addError(Mage::helper('checkout')->__($e->getMessage()));
$this->_redirect('checkout/cart');
}
}
}
}
?>