I want to add text area displayed on product page for all products, when a customer enter any data to that textarea, that data will be shown in the order information. I override cart controller but there is a problem. Here is my code:
Create a file app/etc/MyExtensions_CommentBox.xml
<?xml version="1.0"?> <config> <modules> <MyCommentBox_CommentBox> <active>true</active> <codePool>local</codePool> <depends> <Mage_Checkout/> </depends> </MyCommentBox_CommentBox> </modules> </config>
Create a file app/code/local/MyExtensions/CommentBox/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?><config> <modules> <MyExtensions_CommentBox> <version>0.1.0</version> </MyExtensions_CommentBox> </modules> <frontend> <routers> <checkout> <args> <modules> <MyExtensions_CommentBox before="Mage_Checkout">MyExtensions_CommentBox</MyExtensions_CommentBox </modules> </args> </checkout> </routers> </frontend> </config>
Create a file app/code/local/MyExtensions/CommentBox/controllers/CartController.php
require_once 'Mage/Checkout/controllers/CartController.php'; class MyCommentBox_CommentBox_CartController extends Mage_Checkout_CartController { public function addAction() { if (!$this->_validateFormKey()) { $this->_goBack(); return; } $cart = $this->_getCart(); $params = $this->getRequest()->getParams(); try { if (isset($params['qty'])) { $filter = new Zend_Filter_LocalizedToNormalized( array('locale' => Mage::app()->getLocale()->getLocaleCode()) ); $params['qty'] = $filter->filter($params['qty']); } $product = $this->_initProduct(); $related = $this->getRequest()->getParam('related_product'); /** * Check product availability */ if (!$product) { $this->_goBack(); return; } /****************/ /* Custom Params*/ /****************/ //Create custom option $additionalOptions = array(array( 'code' => 'my_code', 'label' => 'This text is displayed through additional options', 'value' => $this->getRequest()->getParam('textOpt'); )); //Add Custom Option to product $product->addOption($item->addOption(array( 'code' => 'additional_options', 'value' => serialize($additionalOptions), )); /****************/ $cart->addProduct($product, $params); if (!empty($related)) { $cart->addProductsByIds(explode(',', $related)); } $cart->save(); $this->_getSession()->setCartWasUpdated(true); /** * @todo remove wishlist observer processAddToCart */ Mage::dispatchEvent('checkout_cart_add_product_complete', array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse()) ); if (!$this->_getSession()->getNoCartRedirect(true)) { if (!$cart->getQuote()->getHasError()) { $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName())); $this->_getSession()->addSuccess($message); } $this->_goBack(); } } catch (Mage_Core_Exception $e) { if ($this->_getSession()->getUseNotice(true)) { $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage())); } else { $messages = array_unique(explode("\n", $e->getMessage())); foreach ($messages as $message) { $this->_getSession()->addError(Mage::helper('core')->escapeHtml($message)); } } $url = $this->_getSession()->getRedirectUrl(true); if ($url) { $this->getResponse()->setRedirect($url); } else { $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl()); } } catch (Exception $e) { $this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.')); Mage::logException($e); $this->_goBack(); } } }
add code to addtocart.phtml
< textarea rows=4 cols="50" name="textOpt">
When I enter any data to textarea and click the "add to cart" button, it shows "server error 500" on Chrome or Blank page on Firefox. If i remove this code, everything is ok.
/****************/
/* Custom Params*/
/****************/
//Create custom option
$additionalOptions = array(array(
'code' => 'my_code',
'label' => 'This text is displayed through additional options',
'value' => $this->getRequest()->getParam('textOpt');
));
//Add Custom Option to product
$product->addOption($item->addOption(array(
'code' => 'additional_options',
'value' => serialize($additionalOptions),
));
/****************/
Is there a problem with this code?