Adding a custom comment box for each item in the cart is actually very easy.
First lets add the textarea field for each item.
open file: template/checkout/cart.phtml
Add the new heading along with other heading for cart items.
<th><?php echo $this->__('Comments') ?></th>
Open file: template/checkout/cart/item/default.phtml
Add a new column
<td class="a-center"> <textarea name="cart[<?php echo $_item->getId() ?>][comments]" rows="3" cols="20"><?php echo $_item->getItemcomment() ?></textarea> </td>
new we need to add this comment in DB so for that we need to add ‘itemcomment’ field in table ‘sales_flat_quote_item’
now open file : app/code/core/Mage/Checkout/Model/Cart.php
now we need to make changes in function updateItems()
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);
}
/* Start: Custom code added for comments */
if(!empty($itemInfo['comments'])) {
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
# make the frame_queue active
$query = "UPDATE `sales_flat_quote_item` SET itemcomment = '".$itemInfo['comments']."' where item_id = $itemId";
$write->query($query);
$item->setItemcomment($itemInfo['comments']);
}
/* End: Custom code added for comments */
}
Mage::dispatchEvent('checkout_cart_update_items_after', array('cart'=>$this, 'info'=>$data));
return $this;
}
we need to create new function given below in file app/code/core/Mage/Adminhtml/Block/Sales/Order/View/Items.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";
# For older versions of Magento
/* $query = "SELECT q.* FROM `sales_order_entity_int` o
LEFT JOIN `sales_flat_quote_item` q on o.value = q.entity_id
WHERE o.entity_id = $itemId AND o.attribute_id = 343"; */
$res = $write->query($query);
while ($row = $res->fetch() ) {
if(key_exists('itemcomment',$row)) {
echo nl2br($row['itemcomment']);
}
}
}
To add the comments column to the items edit the .phtml file below:
app/design/adminhtml/default/default/template/sales/order/view/items.phtml
Adding header for items to make it look like below:
<tr class="headings">
<th><?php echo $this->helper('sales')->__('Product') ?></th>
<th><?php echo $this->helper('sales')->__('Comments') ?></th>
<th><?php echo $this->helper('sales')->__('Item Status') ?></th>
Adding Column with comments. app/design/adminhtml/default/default/template/sales/order/view/items/renderer/default.phtml
Add a column for item comments juts before status columns to make it look a like below
<td><?php echo $this->getItemcomment($_item) ?></td> <!-- New column added for item comments --> <td class="a-center"><?php echo $_item->getStatus() ?></td>Tags: Magento, Magento snippets
