2

I would like to add the total cart weight onto the order confirmation. On Magento 1 this code was added-

<td colspan="4" align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;">
            <strong><?php echo $this->__('Total UPS Weight:'); ?></strong><?php echo " " . $_order->getWeight(); ?>
        </td>

So it looked like-

 <?php $i=0; foreach ($_order->getAllItems() as $_item): ?>
<?php if($_item->getParentItem()) continue; else $i++; ?>
<tbody<?php echo $i%2 ? ' bgcolor="#F6F6F6"' : '' ?>>
    <?php echo $this->getItemHtml($_item) ?>
</tbody>
<?php endforeach; ?>

<tbody>
   <tr>
        <td colspan="4" align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;">
            <strong><?php echo $this->__('Total UPS Weight:'); ?></strong><?php echo " " . $_order->getWeight(); ?>
        </td>
   </tr>
</tbody>

<tbody>
    <?php echo $this->getChildHtml('order_totals') ?>
</tbody>

On the email/items.phtml file and it worked great. The same code in Magento 2 does not work. I added the code to current Magento 2 code so it looks like-

<?php foreach ($_items as $_item): ?>
        <?php if (!$_item->getParentItem()) : ?>
            <tbody>
                <?= $block->getItemHtml($_item) ?>
            <tr>
        <td align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;">
            <strong><?php echo $this->__('Total UPS Weight:'); ?></strong><?php echo " " . $_order->getWeight(); ?>
        </td>
   </tr>
            </tbody>
        <?php endif; ?>
    <?php endforeach; ?>

When I get the order confirmation it just says-

Error filtering template: Invalid method Magento\Sales\Block\Order\Email\Items::__

  • tjjen, did you try my solution ? – Pawan Oct 9 '18 at 16:22
1
+50

Copy the below file:

vendor/magento/module-sales/view/frontend/templates/email/items.phtml

to your theme location like below:

app/design/frontend/Vendor/theme/Magento_Sales/templates/email/items.phtml

And use the below code for your items.phtml:

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php $_order = $block->getOrder() ?>
<?php if ($_order): ?>
    <?php $_items = $_order->getAllItems(); ?>
    <table class="email-items">
        <thead>
            <tr>
                <th class="item-info">
                    <?= /* @escapeNotVerified */  __('Items') ?>
                </th>
                <th class="item-qty">
                    <?= /* @escapeNotVerified */  __('Qty') ?>
                </th>
                <th class="item-price">
                    <?= /* @escapeNotVerified */  __('Price') ?>
                </th>
            </tr>
        </thead>
        <?php $totalWeight = 0; ?>
        <?php foreach ($_items as $_item): ?>
            <?php if (!$_item->getParentItem()) : ?>
                <tbody>
                    <?= $block->getItemHtml($_item) ?>
                </tbody>
                <?php $totalWeight += $_item->getWeight()*$_item->getQtyOrdered() ?>
            <?php endif; ?>
        <?php endforeach; ?>
        <tbody>
            <tr>
                <td colspan="4" align="left" valign="top" style="font-size:11px; padding:3px 9px; border-bottom:1px dotted #CCCCCC;">
                    <strong><?php echo __('Total UPS Weight:'); ?> </strong><?= $totalWeight ?>
                </td>
           </tr>

        </tbody>
        <tfoot class="order-totals">
            <?= $block->getChildHtml('order_totals') ?>
        </tfoot>
    </table>
    <?php if ($this->helper('Magento\GiftMessage\Helper\Message')->isMessagesAllowed('order', $_order, $_order->getStore()) && $_order->getGiftMessageId()): ?>
        <?php $_giftMessage = $this->helper('Magento\GiftMessage\Helper\Message')->getGiftMessage($_order->getGiftMessageId()); ?>
        <?php if ($_giftMessage): ?>
            <br />
            <table class="message-gift">
                <tr>
                    <td>
                        <h3><?= /* @escapeNotVerified */  __('Gift Message for this Order') ?></h3>
                        <strong><?= /* @escapeNotVerified */  __('From:') ?></strong> <?= $block->escapeHtml($_giftMessage->getSender()) ?>
                        <br /><strong><?= /* @escapeNotVerified */  __('To:') ?></strong> <?= $block->escapeHtml($_giftMessage->getRecipient()) ?>
                        <br /><strong><?= /* @escapeNotVerified */  __('Message:') ?></strong>
                        <br /><?= $block->escapeHtml($_giftMessage->getMessage()) ?>
                    </td>
                </tr>
            </table>
        <?php endif; ?>
    <?php endif; ?>
<?php endif; ?>

Hope this helps!

  • It gets me closer. The only problem I currently have is that it doesn't take quantity into consideration. So if I have 2 the weight on the item needs to be double. Any ideas? Thanks for the help on this. – tjjen Oct 8 '18 at 16:14
  • Can you check the updated answer? – Sukumar Gorai Oct 8 '18 at 16:51
  • I added a few items to test and all I get on the confirmation is Total UPS Weight: 0 – tjjen Oct 8 '18 at 17:13
  • Can you sales_order_item table if it stores the weight correctly or not? Also check of the products have correct weight or not? I have edited my answer. Can you please check. – Sukumar Gorai Oct 8 '18 at 17:20
  • I believe it store the weight correctly, I get all of my shipping rates just fine and your first attempt worked well just not with quantity. I tried the most recent code and it pulled up the 0 for weight again. – tjjen Oct 8 '18 at 19:20
0

You Need to modify 2 files if you want to add a column to item detail named Weight.

1: vendor/magento/module-sales/view/frontend/templates/email/items.phtml

Overwrite this file:

    <thead>
        <tr>
            <th class="item-info">
                <?= /* @escapeNotVerified */  __('Items') ?>
            </th>
            <th class="item-qty">
                <?= /* @escapeNotVerified */  __('Qty') ?>
            </th>
            <th class="item-price">
                <?= /* @escapeNotVerified */  __('Price') ?>
            </th>
            <th class="item-weight">
                <?= /* @escapeNotVerified */  __('Weight') ?>
            </th>
        </tr>
    </thead>

2: vendor/magento/module-sales/view/frontend/templates/email/items/order/default.phtml:39 ** if item is_virtual weight will be return 0**

<td class="item-qty"><?= /* @escapeNotVerified */  $_item->getQtyOrdered() * 1 ?></td>
<td class="item-price">
    <?= /* @escapeNotVerified */  $block->getItemPrice($_item) ?>
</td>
<td class="item-weight">
    <?= /* @escapeNotVerified */  $_item->getWeight(); /**/ ?>
</td>
0

Copy items.phtml from

{Magento2Root}/vendor/magento/module-sales/view/frontend/templates/email/items.phtml

to

app/design/frontend/Vendor/theme/Magento_Sales/templates/email/items.phtml

Add new th just after price

<th class="item-weight">
    <?= /* @escapeNotVerified */  __('Total UPS Weight:') ?>
</th>

Above code will add a new heading to table, Now we need to render weight in td for each ordered item row.

For that we need to copy

{Magento2Root}/vendor/magento/module-sales/view/frontend/templates/email/items/order/default.phtml

to

{Magento2Root}/app/design/frontend/Vendor/theme/Magento_Sales/templates/email/items/order/default.phtml

Add below code after td class="item-price"

<?php if (!$_item->getParentItem()) { ?>
    <td class="item-weight">
        <?php $totalWeight += $_item->getWeight() * $_item->getOrderedQty()* 1  ?>
    </td>
<?php } ?>

Hope above will help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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