Question

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 ?

Was it helpful?

Solution

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());

OTHER TIPS

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().

The $_order->formatPrice() method worked.

$regular_price = $productObj->getPrice('regular_price');
$_order->formatPrice($regular_price);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top