0

In Multiple product i can't able to add in cart

controller.php

public function addtocartAction(){
      /* Get Product id From Form Post */
        $product_id = $this->getRequest()->getPost('productids');
      /* Get Product id From Form Post */

      /* Add Product in to Cart */
        $product=new Mage_Catalog_Model_Product();
        $product->load($product_id); // Product Id
        $quote = Mage::getSingleton('checkout/session')->getQuote();
        $quote->addProduct($product); // quantity is 1
        $cart = Mage::getSingleton('checkout/cart');
        $cart->init(); // tried commenting this too!
        $cart->save();
        Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
        $quote->collectTotals()->save();
        $this->_redirectReferer();
        Mage::getSingleton('core/session')->addSuccess('Your product has been Added successfully...'); 
      /* Add Product in to Cart */
    }


productids contain 1,2,3 but i can add only one product what is the problem here?

3 Answers 3

1

I dont know but you can try following code and also dont use new its a bad practice

public function addtocartAction(){
  /* Get Product id From Form Post */
    $product_ids =  explode(",",$this->getRequest()->getPost('productids')) ;
  /* Get Product id From Form Post */
    foreach($product_ids as $product_id){
        /* Add Product in to Cart */
        $product= Mage::getModel('catalog/product');
        $product->load($product_id); // Product Id
        $quote = Mage::getSingleton('checkout/session')->getQuote();
        $quote->addProduct($product); // quantity is 1
        $cart = Mage::getSingleton('checkout/cart');
        $cart->init(); // tried commenting this too!
        $cart->save();
        Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
        $quote->collectTotals()->save();
        $this->_redirectReferer();
        Mage::getSingleton('core/session')->addSuccess('Your product has been Added successfully...'); 
      /* Add Product in to Cart */
    }

}
4
  • I can't understand this dont use new
    – Magento 2
    Sep 15, 2016 at 5:38
  • forgot that I made other changes look at that and let me know Sep 15, 2016 at 5:39
  • if you want to call new Mage_Catalog_Model_Product(); then magento has its own syntax for calling model, block and helper calss and in your case you can use Mage::getModel('catalog/product') it has same effect as yours code but it is with magento syntax Sep 15, 2016 at 5:40
  • You are always welcome :) Sep 15, 2016 at 5:46
0

productids is array so you do this

 $product->load($product_id[0]);

to enter first product

0

Add additional product to cart using observer events

Add below to your extension's config.xml

      <checkout_cart_save_after>
    <observers>
        <checkout_cart_save_after_handler> <!-- identifier of the event handler -->
        <type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
        <class>customprice/observer</class> <!-- observers class alias -->
        <method>additionalAddtocart</method>  <!-- observer's method to be called -->
        <args></args> <!-- additional arguments passed to observer -->
        </checkout_cart_save_after_handler>
    </observers>
  </checkout_cart_save_after> 

In Observer.php

public function additionalAddtocart(Varien_Event_Observer $observer)
{   
    $form_data=Mage::app()->getRequest()->getParams();
    if(isset($form_data['additionalproducts']) && $form_data['additionalproducts'] !=''){ 
        $products=explode(',',$form_data['additionalproducts']); // passing additional products ids with comma seprate from product page in hidden field.
        foreach($products as $product_id){
            $product = Mage::getModel('catalog/product')->load($product_id);
            $quote = Mage::getSingleton('checkout/session')->getQuote();
            $qty=1;// change as per your task
            $totalprice;// using your price calculation logic here and pass price
            $quoteItem = $quote->addProduct($product, $qty);
            $quoteItem->setCustomPrice($totalprice);// Use it when want update price
            $quoteItem->setOriginalCustomPrice($totalprice); // Use it when want update price
            $quote->collectTotals()->save();
        }
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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