3
view/frontend/layout/sales_email_order_shipment_renderers.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" label="Email Creditmemo Items List" design_abstraction="custom">
    <body>
        <referenceBlock name="sales.email.order.shipment.renderers.default">
            <action method="setTemplate" >
                <argument name="template" xsi:type="string">Vendor_Sales::email/items/shipment/default.phtml</argument>
            </action>
        </referenceBlock>

        <referenceBlock name="sales.email.order.shipment.renderers.grouped">
            <action method="setTemplate" >
                <argument name="template" xsi:type="string">Vendor_Sales::email/items/shipment/default.phtml</argument>
            </action>
        </referenceBlock>

        <referenceBlock name="sales.email.order.shipment.renderers.bundle">
            <action method="setTemplate" >
                <argument name="template" xsi:type="string">Vendor_Sales::email/items/shipment/default.phtml</argument>
            </action>
        </referenceBlock>

        <referenceBlock name="sales.email.order.shipment.renderers.downloadable">
            <action method="setTemplate" >
                <argument name="template" xsi:type="string">Vendor_Sales::email/items/shipment/default.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

file frontend/templates/email/items/shipment/default.phtml

<?php
/** @var $_item \Magento\Sales\Model\Order\Item */
$_item         = $block->getItem();
$_order        = $_item->getOrder();
$helper        = $this->helper('Mod\Sales\Helper\Data');
$product_image = $helper->getProductImage($_item->getSku());
$productObj    = $helper->getProductBySku($_item->getSku());
$color         = $productObj->getAttributeText('color');
$size          = $productObj->getAttributeText('size');
$brand         = $productObj->getDisplayBrand();
?>
    <tr>
    <!-- Price -->
    <div align="left" style="font-size:16px;">
        <?php echo $block->getItemPrice($_item) ?>
    </div>
    <!-- Spacer -->
    </td>
</tr>
    <tr>
        <td height="25">
            &nbsp;
        </td>
    </tr>

Error: Call to a member function setItem() on bool in vendor/magento/module-sales/Block/Order/Email/Items/DefaultItems.php:118 Stack trace: #0

Any thoughts why it shows such error & how to fix it asap ?

2
  • any thoughts on this ?
    – devhs
    Mar 10 at 6:07
  • any thoughts on this ?
    – devhs
    Mar 10 at 11:30
2
+25

That error explains that your block is not an object.

Checking the code:

/**
 * Get the html for item price
 *
 * @param OrderItem|InvoiceItem|CreditmemoItem $item
 * @return string
 * @throws LocalizedException
 */
public function getItemPrice($item)
{
    $block = $this->getLayout()->getBlock('item_price');
    $item->setRowTotal((float) $item->getPrice() * (float) $this->getItem()->getQty());
    $item->setBaseRowTotal((float) $item->getBasePrice() * (float) $this->getItem()->getQty());
    $block->setItem($item);
    return $block->toHtml();
}

$block = $this->getLayout()->getBlock('item_price'); doesn't return the block. You will have to add the below code to your layout.

        <referenceBlock name="items">
            <block class="Magento\Sales\Block\Order\Email\Items\DefaultItems" name="item_price" template="Magento_Sales::email/items/price/row.phtml"/>
        </referenceBlock>

UPDATE

Below should work. The idea is to pass the item_price block to the sales.email.order.shipment.renderers.default block.

    <referenceBlock name="sales.email.order.shipment.renderers.default">
        <block class="Magento\Sales\Block\Order\Email\Items\DefaultItems" name="item_price" template="Magento_Sales::email/items/price/row.phtml"/>
        <action method="setTemplate" >
            <argument name="template" xsi:type="string">Vendor_Sales::email/items/shipment/default.phtml</argument>
        </action>
    </referenceBlock>

OR best way would be to do this:

Create a file view/frontend/layout/sales_email_order_shipment_items.xml and then have this content:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" label="Email Invoice Items List" design_abstraction="custom">
    <update handle="sales_email_item_price"/>
</page>

UPDATE 2

I see you just want to get the price of the item and format it, then you can do this way:

$_order->formatPrice($_item->getPrice());
7
  • hello, where should i add that layout code ?
    – devhs
    Mar 9 at 10:52
  • I have override "sales_email_order_shipment_renderers.xml" in module
    – devhs
    Mar 9 at 10:53
  • Can you add your full override codes, please? Mar 9 at 22:24
  • sure let me add that.
    – devhs
    Mar 10 at 4:38
  • Added full code in question
    – devhs
    Mar 10 at 4:41
0

Your $_item variable / object is invalid. module-sales/Block/Order/Email/Items/DefaultItems is expecting an OrderItem/InvoiceItem/CreditmemoItem object but you are passing a boolean value in $_item.

You need to look at why your shipment/default.phtml template is not returning the correct object from $block->getItem().

4
  • Thanks for the hint. Can you please tell me how can i debug it ? i am sure that $_item is an product object.
    – devhs
    Mar 4 at 12:55
  • If $_item is a product object try echoing $_item->getSku(); in your template to test it.
    – paj
    Mar 4 at 13:42
  • yes i get the sku
    – devhs
    Mar 4 at 14:29
  • any thoughts on this ?
    – devhs
    Mar 5 at 5:58
0

The $_order->formatPrice() method worked.

$regular_price = $productObj->getPrice('regular_price');
$_order->formatPrice($regular_price);
6
  • This will fail if your product is deleted from store. Mar 17 at 22:45
  • And also, if your product's price is changed it will show wrong info. Never get the information from the product object in the sales order section. Mar 17 at 22:51
  • Hi, thanks for the comment what should be the right approach ?
    – devhs
    Mar 18 at 4:52
  • 1
    Check my above answer. magento.stackexchange.com/questions/332113/… Mar 18 at 5:18
  • Thankyou, i will update it
    – devhs
    Mar 18 at 5:22

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.