Add product to basket with custom parameters

I need to create a (virtual, simple) product, and then add it to the basket both programmatically and still. Now I have to set special parameters when this product adds to the cart. but nothing happens. here is my code

$product = Mage::getModel('catalog/product')->load($product_id); $cart = Mage::getModel('checkout/cart'); $cart->init(); $params = array( 'product' => $product->getId(), // This would be $product->getId() 'qty' => 1, 'options' => array( 34 => "value", 35 => "other value", 53 => "some other value" ) ); try { $cart->addProduct($product, new Varien_Object($params)); Mage::getSingleton('checkout/session')->setCartWasUpdated(true); $cart->save(); } catch (Exception $ex) { echo $ex->getMessage(); } 
+4
source share
2 answers

Here is the code I received with success.

 $a_options = array( 'options' => array( 'label' => 'Choice', 'value' => $pkg_selected_products, ) ); $quoteItem->addOption(new Varien_Object( array( 'product' => $quoteItem->getProduct(), 'code' => 'additional_options', 'value' => serialize($a_options) ) )); $quote->addItem($quoteItem); $quote->save(); 
+6
source

I am having problems with the custom date field to add a product to my controller function.

So, I have to break / blow the Magento path and put it in params in AddProduct() as shown below.

  try { $cart = Mage::getModel('checkout/cart'); $previousItemCount = $cart->getQuote()->getItemsCount(); if ($previousItemCount <= 0) { $cart->init(); } $params = $this->getRequest()->getParams(); $product = Mage::getModel('catalog/product')->load($params['product_id']); $date = explode('/', $params['product_dtinvoice']); $date = array( 'month' => $date[0], 'day' => $date[1], 'year' => $date[2], ); $cart->addProduct( $product, new Varien_Object(array( 'product' => $product->getId(), 'qty' => 1, 'options' => array( '4' => array( 'month' => $date['month'], 'day' => $date['day'], 'year' => $date['year'] ), '2' => $params['product_ean'], '3' => $params['product_serialnumber'], '1' => $params['product_seller'], ), )) ); $cart->save(); if ($previousItemCount < $cart->getQuote()->getItemsCount()) { $return = array('result' => true, 'msg' => ''); } else { $return = array('result' => false, 'msg' => 'Did not possible to add this product to cart. Please contact the administrator'); } $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($return)); } catch(Exception $e) { Mage::throwException($e->getMessage()); } 
0
source

All Articles