0

I need to add comment to cart page using observer . I need to know better way. Is it better to add custom option. i have tried using following code but no success

$option = array(
'is_delete' => 0,
'is_require' => false,
'previous_group' => 'text',
'title' => 'Shipping Description',
'type' => 'field',
'price_type' => 'fixed',
'price' => '0.0000'
);
 $options->addOption($option);
$options->saveOptions();

I am using checkout_cart_product_add_after observer . comment is only for display purpose so it is custom option is better or any other option

1
  • how can i Add custom option after product add to cart ?? Nov 21, 2013 at 7:38

4 Answers 4

1

If you want to add the possibility for the customer to add a comment, you can greatly use the "add gift message" option, which magento offers out of the box. Just change the translation

0
0

In your theme, for the file: template/checkout/cart.phtml
Add the new heading along with other heading for cart items.

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

In the 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>

For Older version of Magento it would be:

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

The next step is to save the comment in DB, when customer update the cart. So add a new field ‘itemcomment’ in the tabel ‘sales_flat_quote_item’. (For older version of Magento the table would be ‘sales_quote_item’)

Now we are going to add the code which will do the DB operation. For this we will need to modify the file:app/code/core/Mage/Checkout/Model/Cart.php (Note: If you are planning to upgrade your Magento setup, copy this file to local & modify.)
Here we need to add some code to the function updateItems(), such a way that the function should now look like below:

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;
}

Showing the comment in Admin -> View Order
Add a new function getItemcomment() to the file below:
app/code/core/Mage/Adminhtml/Block/Sales/Order/View/Items.php

If you are on verstion 1.5 or later.. add it to the file below.
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

Add header for items:

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

To add Column with comments edit the .phtml file below:
app/design/adminhtml/default/default/template/sales/order/view/items/renderer/default.phtml
Add a column for item comments.

<td><?php echo $this->getItemcomment($_item) ?></td> <!-- New column added for item comments -->
0

There's a free extension that you should take a look at: http://www.magentocommerce.com/magento-connect/magemaven-ordercomment.html

0

I have found the solution from this link

Creating Custom option and prize to just added product to cart using observer. It might help you out.

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