0

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:

  1. 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>
    
  2. 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>
    
  3. 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();
            }
         }
    
        }
    
  4. 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?

1
  • $item variable is not defined. – Danil Aug 23 '16 at 16:44
0

This is Magento default functionality

An example of how to use it:

  1. Go to Magento admin panel -> Catalog -> Manage Products
  2. Click the "Edit" link in grid.
  3. Add the custom option to it(See left navigation): Title - "Text Area name", Input Type - "Area", Sort Order - "1".
  4. Click the "Save And Continue Edit " button.
  5. Navigate to your product in the Front end.

See screen shot

Admin Side

enter image description here

Frontend enter image description here

2
  • yes i know it is Magento default functionality but i don't want to use magento custom option. thank you. – jennifer123 Dec 19 '15 at 2:30
  • @jennifer123, Don't know if you have solved this in any way, but if you don't want to use default custom options feature, then you can't use that code either. It is only for that feature(Not sure if you are using that in correct way either!). You'll need to create a heavy module that will save that information along with the order item in custom tables or in sale_flat_order_item table. – Prateek Dec 22 '15 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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