31
Oct

Add Product Text Field with Survey ID in Cart

share

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.
text field dynamically

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.

admin


Subscribe to get the latest blogs, insights, and innovations.

    By submitting this form, you agree to Ficode Technologies Limited Privacy Policy

    Linkedin Posts

    Change default  controller  and default login in yii2 advanced template

    Change default controller and default login in yii2 advanced template

    previous-blog-arrowPrevious
    Yii2 Pretty URL / SEO Friendly URL - Advanced Template

    Yii2 Pretty URL / SEO Friendly URL - Advanced Template

    next-blog-arrowNext