Sometimes we need to add multiple simple type products to cart programmatically from listing page or using ajax. To do that we need to do some modification in listing page html so required data will be post using add button.
Below is the function we can add to add simple type products with quantity -
public function addtocartAction(){
$productIds = $this->getRequest()->getParam('products');
$cart = Mage::getModel('checkout/cart');
$cart->init();
foreach( $productIds as $product_id ) {
if( $product_id ){
$product = Mage::getModel('catalog/product')->load($product_id);
try {
$cart->addProduct($product, array('qty' => '1'));
} catch (Exception $e) {
Mage::log( $e->getMessage() );
}
}
}
$cart->save();
}
In above function products is the posted field contains array of selected product ids and qty is the parameter for quantity.
0 Comment(s)