Oct
Add Product Text Field with Survey ID in Cart
I had to add a survey id dynamically as a custom option while adding a product to the cart programmatically.
For this,
I first created a custom option for the typing text field for the product I wanted to add.
Then on my controller’s addtocartAction, I wrote this code:
$qty = 1;
$survey_id = Mage::app()->getRequest()->getParam('sid');
$sku='surveycheckout';
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
$productid=$product->getId();
$session= Mage::getSingleton('checkout/session');
$quote = $session->getQuote();
$params['qty'] = $qty;
foreach ($product->getProductOptionsCollection() as $o) {
$optionid = $o->getOptionId();
break;
}
$str="Survey ID : ".$survey_id;
$params['options']=array(
$optionid => $str,
);
$cart=Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($productid,$params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
Mage::dispatchEvent('checkout_cart_add_product_complete',
array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
);
That’s it, the product gets added with the survey id in the cart.
We can also use this approach, if we need to save a string with the order, instead of creating an extra attribute for the sales/order model.


