Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Magento- Add Comment Field In Cart

    • 0
    • 1
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 7.41k
    Comment on it

    Hello Readers, In this blog tutorial i will explain how to add custom comment field box to each product in view shopping cart page.

    Follow the below steps to create magento comment field in cart & save field data into DB

    1. Add new heading in cart items. (Path: current_theme/default/template/checkout/cart.phtml)

    <th><?php echo $this->__('Comments') ?></th>

    2. Add custom comment field textarea for each product in cart.

    go to current_theme/default/template/checkout/cart/item/default.phtml & add the following code.

    <td class="a-center">
    
    <textarea name="cart[<?php echo $_item->getId() ?>][comments]" rows="3" cols="20"><?php echo $_item->getItemcomment() ?></textarea>
    
    </td>

    3. Save comment field text data in DB whenever customer update the cart.

    To add comment data in DB we need to add new field 'itemcomment' in table ‘sales_flat_quote_item’.

    Now go to app/code/core/Mage/Checkout/Model/Cart.php & add below code in your cart model which perform the DB operation.

    public function updateItems($data)
    
     {
    
         Mage::dispatchEvent('checkout_cart_update_items_before', array('cart'=>$this, 'info'=>$data));
    
    
         foreach ($data as $itemId => $itemInfo) {
    
    
             $item = $this->getQuote()->getItemById($itemId);
    
             if (!$item) {
    
                 continue;
    
             }
    
    
             if (!empty($itemInfo['remove']) || (isset($itemInfo['qty']) && $itemInfo['qty']=='0')) {
    
                 $this->removeItem($itemId);
    
                 continue;
    
             }
    
    
             $qty = isset($itemInfo['qty']) ? (float) $itemInfo['qty'] : false;
    
             if ($qty > 0) {
    
                 $item->setQty($qty);
    
             }
    
    
         if(!empty($itemInfo['comments'])) {
    
    
          $write = Mage::getSingleton('core/resource')->getConnection('core_write');
    
         $query = "UPDATE `sales_flat_quote_item` SET itemcomment = '".$itemInfo['comments']."' where item_id = $itemId";
    
        $write->query($query);
    
    
          $item->setItemcomment($itemInfo['comments']);
    
         }
    
    }
    
         Mage::dispatchEvent('checkout_cart_update_items_after', array('cart'=>$this, 'info'=>$data));
    
         return $this;
    
     }

    4. Add a new function getItemcomment() to the below file.

    Path: app/code/core/Mage/Adminhtml/Block/Sales/Items/Abstract.php 

    public function getItemcomment($item) {
    
      $itemId = $item->getId();
    
    $write = Mage::getSingleton('core/resource')->getConnection('core_write');
    
    $query = "SELECT q.* FROM `sales_flat_order_item` o
    
        LEFT JOIN `sales_flat_quote_item` q on o.quote_item_id = q.item_id
    
        WHERE o.item_id = $itemId";  
    
     $res = $write->query($query);
    
    while ($row = $res->fetch() ) {
    
       if(key_exists('itemcomment',$row)) {
    
        echo nl2br($row['itemcomment']);
    
       }
    
      }
    
     }  
    
    


    5. Add comment column in admin view order section.

    Go to app/design/adminhtml/default/default/template/sales/order/view/items.phtml & add below code just before  <th><?php echo $this->helper('sales')->__('Item Status') ?></th>

    <th><?php echo $this->helper('sales')->__('Comments') ?></th>

    6. Render/fetch comments information in comment column (which has been created in step 5 through helper):

    Go to app/design/adminhtml/default/default/template/sales/order/view/items/renderer/default.phtml

    <td><?php echo $this->getItemcomment($_item) ?></td>

     

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: