2

I know that we can use custom options to add form fields to a specific product page on frontend, but i want a textarea which should be displayed on front for all products on the product page.Currently using custom options will require to create it for all products, but i was such an option which is shown at front end automatically. When a customer enter any data to that textarea, that data will be shown in the order information, just like the custom options.

Any help and idea will be appreciated.

Thank you

2 Answers 2

5

You can create inside form of product page your input:

1- Add your text area in product Page

  <textarea rows="4" cols="50" name="textOpt">

When you add the product in cart you can see the headers:

form_key:ITpMa08mBS3jpqyP
product:337
related_product:
qty:1
textOpt:test2

2- Extend Cart Controller

require_once 'Mage/Checkout/controllers/CartController.php';


class NameSpace_ModuleName_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();
    }
 }

}

3- Verify Options

this modification will work on all products. You can validate that the parameter exists to prevent the options are created

2

What you are looking for is not actually a product option, but more a order comment for each product on the product view page.

What you basically need to do is:

  1. create an order item attribute
  2. store the text, entered on the product page, to the quote item
  3. convert the quote items: sales_convert_quote_item_to_order_item event should be your friend for this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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