0

I am receiving error when I use plugin to modify Invoice Items template.

Exception #0 (RuntimeException): Renderer list for block "sales.order.invoice" is not defined

This seems like a weird bug, I tried to add sequence in module.xml for Magento_Sales, but still gives the error.

Is there a better way to override this template file?

In my \Vendor\Module\etc\frontend\di.xml

<type name="Magento\Sales\Block\Order\Invoice\Items">
    <plugin name="vendor_module_sales_order_invoice_items" type="Vendor\Module\Plugin\Sales\Order\Invoice\Items" />
</type>

My plugin file \Vendor\Module\Plugin\Sales\Order\Invoice\Items.php

<?php
namespace Vendor\Module\Plugin\Sales\Order\Invoice;

use Vendor\Module\Helper\Data as Helper;

class Items
{
    protected $_helper;

    public function __construct(
        Helper $_helper
    ) 
    {
        $this->_helper = $_helper;
    }

    public function beforeToHtml($subject)
    {
        if(!$this->_helper->isEnabled()) {
            return;
        }

        $subject->setTemplate("Vendor_Module::sales/order/invoice/items.phtml");
        return;
    }
}

Has anyone else encountered this? And is there a better way to workaround this problem? I am using Magento version 2.3.1.

1

The best method of changes the template of a Block class Magento\Sales\Block\Order\Invoice\Items using plugin that getTemplate.

Here plugin code

<?php
namespace Vendor\Module\Plugin\Sales\Order\Invoice;

use Vendor\Module\Helper\Data as Helper;

class Items
{
    protected $_helper;

    public function __construct(
        Helper $_helper
    ) 
    {
        $this->_helper = $_helper;
    }

    public function afterGetTemplate(
        \Magento\Sales\Block\Order\Invoice\Items $subject,
        $result
    ) {
        if(!$this->_helper->isEnabled()) {
          return $result;  
        }

        return "Vendor_Module::sales/order/invoice/items.phtml";
    }   
}

If you want to continue with your beforeToHtml then changes return tpye

namespace Vendor\Module\Plugin\Sales\Order\Invoice;

use Vendor\Module\Helper\Data as Helper;

class Items
{
    protected $_helper;

    public function __construct(
        Helper $_helper
    ) 
    {
        $this->_helper = $_helper;
    }

    public function beforeToHtml($subject)
    {
        if(!$this->_helper->isEnabled()) {
            return [];
        }

        $subject->setTemplate("Vendor_Module::sales/order/invoice/items.phtml");
        $return [];
    }

}
2
  • So @Amit Bera, just out of curiosity, is there any other way to override a template other than using a plugin? I always thought the only way to do it was to override the corresponding layout file or recreating the template in the <theme_dir>. So which is recommended way to do? Would you please shed some light on that. It would be very helpful to me. Thanks in, advance. Mar 15 '20 at 8:36
  • @Amit Bera I agree both those solutions SHOULD be working, but neither of them do, still returns the same error. Exception #0 (RuntimeException): Renderer list for block "sales.order.invoice" is not defined Mar 16 '20 at 7:30
0

You can try different approach
Create new layout file:

Vendor/Module/view/frontend/layout/sales_order_invoice.xml

with 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">
    <body>
        <referenceBlock name="invoice_items">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Vendor_Module::order/invoice/items.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

Create new .phtml file

Vendor/Module/view/frontend/templates/order/invoice/items.phtml

with content that you wanna customize or copied from default Magento.

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.