3

I want to extend my checkout by giving my customers the possibility to write comments for each single product in their cart.

What do I need to do in order to add a field to the cart, that gets stored in the DB and is useable in emails?

Or where would I start to develop here?

Max
  • 1,844
  • 5
  • 30
  • 52
  • 2
    Hi try to use https://github.com/vijays91/Magento-Cart-Comment or https://magento.stackexchange.com/questions/11024/how-to-add-comment-to-cart-page or http://www.magentoworks.net/add-comment-box-to-each-product-in-magento-cart/ – Vadym Kalin May 16 '17 at 08:18
  • Looks promising! Could you write an answer based on your module that also explains how to use that variables in a mail? – Max May 16 '17 at 08:23
  • It is not my module. – Vadym Kalin May 16 '17 at 08:28

1 Answers1

7

If you are using referenced extension https://github.com/vijays91/Magento-Cart-Comment it will give you all the things you need for adding comments and storing and showing them in admin panel.

In addition to that, if you want to send those details to email templates, use below code. This code is based on RWD theme, update as per your theme. You need to copy these files from base/default theme to your theme.

Update your app\design\frontend\rwd\default\layout\learn\cartcomment.xml file and add below code to it under layout tag for new order email.

<sales_email_order_items>
    <reference name="items">
        <action method="setTemplate">
            <value>cartcomment/email/order/items.phtml</value>
        </action>
        <action method="addItemRender"><type>default</type><block>sales/order_email_items_order_default</block><template>cartcomment/email/order/items/order/default.phtml</template></action>
        <action method="addItemRender"><type>grouped</type><block>sales/order_email_items_order_grouped</block><template>email/order/items/order/default.phtml</template></action>
        <block type="sales/order_totals" name="order_totals" template="sales/order/totals.phtml">
            <action method="setLabelProperties"><value>colspan="3" align="right" style="padding:3px 9px"</value></action>
            <action method="setValueProperties"><value>align="right" style="padding:3px 9px"</value></action>
            <block type="tax/sales_order_tax" name="tax" template="tax/order/tax.phtml">
                <action method="setIsPlaneMode"><value>1</value></action>
            </block>
        </block>
    </reference>
</sales_email_order_items>
<sales_email_order_invoice_items>
    <reference name="items">
        <action method="setTemplate">
            <value>cartcomment/email/order/invoice/items.phtml</value>
        </action>
        <action method="addItemRender"><type>default</type><block>sales/order_email_items_default</block><template>cartcomment/email/order/items/invoice/default.phtml</template></action>
        <action method="addItemRender"><type>grouped</type><block>sales/order_email_items_order_grouped</block><template>email/order/items/invoice/default.phtml</template></action>
        <block type="sales/order_invoice_totals" name="invoice_totals" template="sales/order/totals.phtml">
            <action method="setLabelProperties"><value>colspan="3" align="right" style="padding:3px 9px"</value></action>
            <action method="setValueProperties"><value>align="right" style="padding:3px 9px"</value></action>
            <block type="tax/sales_order_tax" name="tax" template="tax/order/tax.phtml">
                <action method="setIsPlaneMode"><value>1</value></action>
            </block>
        </block>
    </reference>
</sales_email_order_invoice_items>
<sales_email_order_shipment_items>
    <reference name="items">
        <action method="setTemplate">
            <value>cartcomment/email/order/shipment/items.phtml</value>
        </action>
        <action method="addItemRender"><type>default</type><block>sales/order_email_items_default</block><template>cartcomment/email/order/items/shipment/default.phtml</template></action>
    </reference>
    <block type="core/text_list" name="additional.product.info" />
</sales_email_order_shipment_items>

<sales_email_order_creditmemo_items>
    <reference name="items">
        <action method="setTemplate">
            <value>cartcomment/email/order/creditmemo/items.phtml</value>
        </action>
        <action method="addItemRender"><type>default</type><block>sales/order_email_items_default</block><template>cartcomment/email/order/items/creditmemo/default.phtml</template></action>
        <action method="addItemRender"><type>grouped</type><block>sales/order_email_items_order_grouped</block><template>cartcomment/email/order/items/creditmemo/default.phtml</template></action>
        <block type="sales/order_creditmemo_totals" name="creditmemo_totals" template="sales/order/totals.phtml">
            <action method="setLabelProperties"><value>colspan="3" align="right" style="padding:3px 9px"</value></action>
            <action method="setValueProperties"><value>align="right" style="padding:3px 9px"</value></action>
            <block type="tax/sales_order_tax" name="tax" template="tax/order/tax.phtml">
                <action method="setIsPlaneMode"><value>1</value></action>
            </block>
        </block>
    </reference>
    <block type="core/text_list" name="additional.product.info" />
</sales_email_order_creditmemo_items>

Now create a file, app\design\frontend\rwd\default\template\cartcomment\email\order\items.phtml

<?php $_order = $this->getOrder() ?>
<?php if ($_order): ?>
<table cellspacing="0" cellpadding="0" border="0" width="650" style="border:1px solid #EAEAEA;">
    <thead>
        <tr>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Item') ?></th>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Sku') ?></th>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Comments') ?></th>
            <th align="center" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Qty') ?></th>
            <th align="right" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Subtotal') ?></th>
        </tr>
    </thead>

    ...............

Now create new file app\design\frontend\rwd\default\template\cartcomment\email\order\items\order\default.phtml

<?php $_item = $this->getItem() ?>
<?php $_order = $this->getItem()->getOrder() ?>
<tr>
    <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;">
        <strong style="font-size:11px;"><?php echo $this->escapeHtml($_item->getName()) ?></strong>
        <?php if ($this->getItemOptions()): ?>
        <dl style="margin:0; padding:0;">
            <?php foreach ($this->getItemOptions() as $option): ?>
            <dt><strong><em><?php echo $option['label'] ?></em></strong></dt>
            <dd style="margin:0; padding:0 0 0 9px;">
                <?php echo nl2br($option['value']) ?>
            </dd>
            <?php endforeach; ?>
        </dl>
        <?php endif; ?>
        <?php $addInfoBlock = $this->getProductAdditionalInformationBlock(); ?>
        <?php if ($addInfoBlock) :?>
            <?php echo $addInfoBlock->setItem($_item)->toHtml(); ?>
        <?php endif; ?>
        <?php echo $this->escapeHtml($_item->getDescription()) ?>
    </td>
    <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"><?php echo $this->escapeHtml($this->getSku($_item)) ?></td>
    <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"><?php echo $this->escapeHtml($_item->getItemcomment()) ?></td>    

            .............

This will add comments to order email. Now to add comments to invoice, shipment and creditmemo, create below files with below contents. app\design\frontend\rwd\default\template\cartcomment\email\order\invoice\items.phtml

<?php $_invoice = $this->getInvoice() ?>
<?php $_order   = $this->getOrder() ?>
<?php if ($_invoice && $_order): ?>
<table cellspacing="0" cellpadding="0" border="0" width="650" style="border:1px solid #EAEAEA;">
    <thead>
        <tr>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Item') ?></th>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Sku') ?></th>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Comments') ?></th>
            <th align="center" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Qty') ?></th>
            <th align="right" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Subtotal') ?></th>
        </tr>
    </thead>
.......

app\design\frontend\rwd\default\template\cartcomment\email\order\items\invoice\default.phtml

<?php $_item = $this->getItem() ?>
<?php $_order = $this->getItem()->getOrder(); ?>
<tr>
    <td align="left" valign="top" style="padding:3px 9px; border-bottom:1px dotted #CCCCCC;">
        <strong style="font-size:11px;"><?php echo $this->escapeHtml($_item->getName()) ?></strong>
        <?php if ($this->getItemOptions()): ?>
        <dl style="margin:0; padding:0;">
            <?php foreach ($this->getItemOptions() as $option): ?>
            <dt><strong><em><?php echo $option['label'] ?></em></strong></dt>
            <dd style="margin:0; padding:0 0 0 9px;"><?php echo nl2br($option['value']) ?></dd>
            <?php endforeach; ?>
        </dl>
        <?php endif; ?>
        <?php $addInfoBlock = $this->getProductAdditionalInformationBlock(); ?>
        <?php if ($addInfoBlock) :?>
            <?php echo $addInfoBlock->setItem($_item->getOrderItem())->toHtml(); ?>
        <?php endif; ?>
        <?php echo $this->escapeHtml($_item->getDescription()) ?>
    </td>
    <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"><?php echo $this->escapeHtml($this->getSku($_item)) ?></td>
    <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"><?php echo $this->escapeHtml($_item->getItemcomment()) ?></td>
......

app\design\frontend\rwd\default\template\cartcomment\email\order\shipment\items.phtml

<?php $_shipment = $this->getShipment() ?>
<?php $_order    = $this->getOrder() ?>
<?php if ($_shipment && $_order): ?>
<table cellspacing="0" cellpadding="0" border="0" width="650" style="border:1px solid #EAEAEA;">
    <thead>
        <tr>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Item') ?></th>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Sku') ?></th>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Comments') ?></th>
            <th align="center" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Qty') ?></th>
        </tr>
    </thead>
.........

app\design\frontend\rwd\default\template\cartcomment\email\order\items\shipment\default.phtml

<?php $_item = $this->getItem() ?>
<tr>
    <td align="left" valign="top" style="font-size:11px; padding:3px 9px;">
        <strong><?php echo $this->escapeHtml($_item->getName()) ?></strong>
        <?php if ($this->getItemOptions()): ?>
        <dl style="margin:0; padding:0;">
            <?php foreach ($this->getItemOptions() as $option): ?>
            <dt><strong><em><?php echo $option['label'] ?></em></strong></dt>
            <dd style="margin:0; padding:0 0 0 9px;"><?php echo nl2br($option['value']) ?></dd>
            <?php endforeach; ?>
        </dl>
        <?php endif; ?>
        <?php $addInfoBlock = $this->getProductAdditionalInformationBlock(); ?>
        <?php if ($addInfoBlock) :?>
            <?php echo $addInfoBlock->setItem($_item->getOrderItem())->toHtml(); ?>
        <?php endif; ?>
        <?php echo $this->escapeHtml($_item->getDescription()) ?>
    </td>
    <td align="left" valign="top" style="font-size:11px; padding:3px 9px;"><?php echo $this->escapeHtml($this->getSku($_item)) ?></td>
    <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"><?php echo $this->escapeHtml($_item->getItemcomment()) ?></td>
    .........

app\design\frontend\rwd\default\template\cartcomment\email\order\creditmemo\items.phtml

<?php $_creditmemo = $this->getCreditmemo() ?>
<?php $_order      = $this->getOrder() ?>
<?php if ($_creditmemo && $_order): ?>
<table cellspacing="0" cellpadding="0" border="0" width="650" style="border:1px solid #EAEAEA;">
    <thead>
        <tr>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Item') ?></th>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Sku') ?></th>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Comments') ?></th>
            <th align="center" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Qty') ?></th>
            <th align="right" bgcolor="#EAEAEA" style="font-size:13px; padding:3px 9px"><?php echo $this->__('Subtotal') ?></th>
        </tr>
    </thead>
.........

app\design\frontend\base\default\template\email\order\items\creditmemo\default.phtml

<?php $_item = $this->getItem() ?>
<?php $_order = $this->getItem()->getOrder(); ?>
<tr>
    <td align="left" valign="top" style="padding:3px 9px; border-bottom:1px dotted #CCCCCC;">
        <strong style="font-size:11px;"><?php echo $this->escapeHtml($_item->getName()) ?></strong>
        <?php if ($this->getItemOptions()): ?>
        <dl style="margin:0; padding:0;">
            <?php foreach ($this->getItemOptions() as $option): ?>
            <dt><strong><em><?php echo $option['label'] ?></em></strong></dt>
            <dd style="margin:0; padding:0 0 0 9px;"><?php echo nl2br($option['value']) ?></dd>
            <?php endforeach; ?>
        </dl>
        <?php endif; ?>
        <?php $addInfoBlock = $this->getProductAdditionalInformationBlock(); ?>
        <?php if ($addInfoBlock) :?>
            <?php echo $addInfoBlock->setItem($_item->getOrderItem())->toHtml(); ?>
        <?php endif; ?>
        <?php echo $this->escapeHtml($_item->getDescription()) ?>
    </td>
    <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"><?php echo $this->escapeHtml($this->getSku($_item)) ?></td>
    <td align="center" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;"><?php echo $_item->getQty()*1 ?></td>
    <td align="right" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;">
        <?php if ($this->helper('tax')->displaySalesPriceExclTax($_order->getStore()) || $this->helper('tax')->displaySalesBothPrices($_order->getStore())): ?>
            <?php if ($this->helper('tax')->displaySalesBothPrices($_order->getStore())): ?>
                <span class="label"><?php echo Mage::helper('tax')->__('Excl. Tax'); ?>:</span>
            <?php endif; ?>
            <?php if (Mage::helper('weee')->typeOfDisplay($_item, array(0, 1, 4), 'email', $_order->getStore())): ?>
                <?php echo $_order->formatPrice($_item->getRowTotal()+$_item->getWeeeTaxAppliedRowAmount()+$_item->getWeeeTaxRowDisposition()); ?>
            <?php else: ?>
                <?php echo $_order->formatPrice($_item->getRowTotal()) ?>
            <?php endif; ?>
.......

EDIT

To add comments on Checkout page.

Add below to app\design\frontend\rwd\default\layout\learn\cartcomment.xml

<checkout_onepage_index translate="label">
    <reference name="checkout_review_sidebar">
        <action method="setTemplate">
            <value>cartcomment/checkout/onepage/review/info.phtml</value>
        </action>
            <action method="addItemRender"><type>default</type><block>checkout/cart_item_renderer</block><template>cartcomment/checkout/onepage/review/item.phtml</template></action>
            <action method="addItemRender"><type>grouped</type><block>checkout/cart_item_renderer_grouped</block><template>cartcomment/checkout/onepage/review/item.phtml</template></action>
            <action method="addItemRender"><type>configurable</type><block>checkout/cart_item_renderer_configurable</block><template>cartcomment/checkout/onepage/review/item.phtml</template></action>
    </reference>
</checkout_onepage_index>

Create below files. app\design\frontend\rwd\default\template\cartcomment\checkout\onepage\review\info.phtml

<?php echo $this->getChildHtml('items_before'); ?>
<div id="checkout-review-table-wrapper">
    <?php $_tableClass = $this->helper('tax')->displayCartBothPrices() ? 'linearize-table-large' : 'linearize-table'; ?>
    <table class="data-table <?php echo $_tableClass; ?> checkout-review-table" id="checkout-review-table">
        <?php if ($this->helper('tax')->displayCartBothPrices()): $colspan = $rowspan = 2; else: $colspan = $rowspan = 1; endif; ?>
        <col />
        <col width="1" />
        <col width="1" />
        <col width="1" />
        <?php if ($this->helper('tax')->displayCartBothPrices()): ?>
        <col width="1" />
        <col width="1" />
        <?php endif; ?>
        <thead>
            <tr>
                <th rowspan="<?php echo $rowspan ?>"><?php echo $this->__('Product') ?></th>
                <th colspan="<?php echo $colspan ?>" class="a-center"><?php echo $this->__('Price') ?></th>
                <th colspan="<?php echo $colspan ?>" class="a-center"><?php echo $this->__('Comments') ?></th>
.........

app\design\frontend\rwd\default\template\cartcomment\checkout\onepage\review\item.phtml

<?php $_item = $this->getItem()?>
<tr>
    <td>....
    </td>
    <td>...</td>
    <?php /*
#######################################
#######################################
*/?>    
<!---- CART COMMENT -->
    <td  class="product-cart-actions a-center" data-rwd-label="<?php echo $this->__('Comments'); ?>">
        <textarea id="cart_<?php echo $_item->getId() ?>_comment" name="cart[<?php echo $_item->getId() ?>][comments]" rows="2" cols="10" maxlength="250" style="margin-bottom:10px;width:200px;" onfocus="$('cart_<?php echo $_item->getId() ?>_comment').nextSiblings()[0].setStyle({'display':'inline-block'});"><?php echo $_item->getItemcomment();?></textarea>

        <button type="submit" name="update_cart_action" value="update_comments" title="<?php echo $this->__('Update'); ?>" class="button btn-update"><span><span><?php echo $this->__('Update'); ?></span></span>
        </button>
    </td>    
<!---- CART COMMENT -->
<?php /*
#######################################
#######################################
*/?>
    ........
Jaimin Sutariya
  • 11,140
  • 5
  • 34
  • 70
  • Looks promising. Could you explain how the mail templates are working together with the XML? – Max May 17 '17 at 07:10
  • Email templates use phtml files to display product details. I have added code in app\design\frontend\rwd\default\layout\learn\cartcomment.xml which is required to update phtml for these templates. – Jaimin Sutariya May 17 '17 at 08:04
  • Could you please also add the possibility to edit the comment in the checkout? That would be cool and I'm absolutely happy with everything. – Max May 18 '17 at 08:41
  • I'll try to update my answer with comments on checkout page. – Jaimin Sutariya May 18 '17 at 09:33
  • 1
    I've updated code to add comment box on checkout page. But it needs to be saved on update button click. I'll add code once I get my hands back on it – Jaimin Sutariya May 19 '17 at 12:43
  • Thank you for the ans but order confirmation email is not coming when order is place – Ashna Mar 08 '18 at 05:42
  • @Afreen, there is no code in above answer which is related to email sending. You can ask a separate question for your query. – Jaimin Sutariya Mar 08 '18 at 08:19