Magento2: Can not Overwrite Core Blocks In Custom Modules

Created on 10 Mar 2016  路  26Comments  路  Source: magento/magento2

Steps to reproduce

  1. Had to add extra table after sales order item for our Booking Module
  2. Tried to overwrite sales_order_item_renderers.xml from my module

    Expected result

  3. After the block override, user should be able to view booking times with new participants details directly below the item purchased

    Easy fix

  4. This issue is due to this line in sales_order_item_renderers.xml

    <referenceBlock name="sales.order.items.renderers">
       <block class="Magento\Sales\Block\Order\Item\Renderer\DefaultRenderer" as="default" template="order/items/renderer/default.phtml"/>
    </referenceBlock>
    

I can not override this if name is not present. If above code change to

    <referenceBlock name="sales.order.items.renderers">
        <block class="Magento\Sales\Block\Order\Item\Renderer\DefaultRenderer" name="sales.order.items.renderer.default" as="default" template="order/items/renderer/default.phtml"/>
    </referenceBlock>

So I can override it by our module

    <referenceBlock name="sales.order.items.renderers">
        <block class="Magento\Sales\Block\Order\Item\Renderer\DefaultRenderer" name="sales.order.items.renderer.default" as="default" template="Stepzerosolutions_Timeslotbooking::order/items/renderer/default.phtml"/>
    </referenceBlock>
Frontend Fixed in 2.3.x Clear Description Confirmed Format is not valid Ready for Work Reproduced on 2.1.x Reproduced on 2.2.x bug report

Most helpful comment

@nobuttsalex your method is working just their is small change is directory structure di.xml file should be in Vendor/Module/etc/di.xml instead of Vendor/Module/etc/frontend/di.xml

Here is the code that i use for override default renderer of email template.

Vendor/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Block\Order\Email\Items\Order\DefaultOrder" type="Vendor\Module\Block\Order\Email\Items\Order\DefaultOrder" />
</config>

Vendor/Module/Block/Order/Email/Items/Order/DefaultOrder.php

class DefaultOrder extends \Magento\Sales\Block\Order\Email\Items\Order\DefaultOrder
{
    public function setTemplate($template) {
        return parent::setTemplate('Vendor_Module::email/items/order/default.phtml');
    }
}

For override default.phtml file copy from magento\module-sales\view\frontend\templates\email\items\order\default.phtml
to your module
Vendor/Module/view/frontend/templates/email/items/order/default.phtml

All 26 comments

This should also apply for the "sales.order.print.renderers"

    <referenceBlock name="sales.order.print.renderers">
        <block class="Magento\Sales\Block\Order\Item\Renderer\DefaultRenderer" as="default" template="order/items/renderer/default.phtml"/>
    </referenceBlock>

Thanks
Nuwinda

This happens when trying to override sales_email_order_renderers.xml under Magento_Bundle.

<referenceBlock name="sales.email.order.renderers">
            <block class="Vendor\Module\Block\Sales\Order\Items\Renderer" as="bundle" template="Magento_Bundle::email/order/items/order/default.phtml"/>
        </referenceBlock>

Doesn't work on the theme level under app/design/Vendor/theme/Magento_Bundle/layout/sales_email_order_renderers.xml

@antonkril can you review?

Another option you have is to set your template to the overriden_templates argument of
<block class="Magento\Sales\Block\Order\Items" name="order_items" template="order/items.phtml">

the same situation in \magento\module-checkout\view\frontend\layout\checkout_cart_item_renderers.xml
<block class="Magento\Checkout\Block\Cart\Item\Renderer" as="simple" template="cart/item/default.phtml"> has no name and can't be overriden.

hello :
nothing is not to be .... i'm using code below and it work fine :

    <block class="Magento\Sales\Block\Order\Item\Renderer\DefaultRenderer" name="sales.order.items.renderer.default" before="-" as="simple" template="[your namespace_  module]:order/items/renderer/default.phtml"/>

Same issue with vendor/magento/module-sales/view/adminhtml/layout/sales_order_view.xml. I'm trying to add an item to the columns argument in the following block, but I cannot reference it in my local module.

<block class="Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer" as="default" template="order/view/items/renderer/default.phtml">

Is there any workaround without modify the core xml?

@dan-andreoli In my case I used a plugin.

Plugin/Sales/Block/Adminhtml/Order/View/Items/Renderer/DefaultRendererPlugin.php

<?php

namespace Vendor\Module\Plugin\Sales\Block\Adminhtml\Order\View\Items\Renderer;

use Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer;

class DefaultRendererPlugin
{
    /**
     * Add the column name to the columns array. Had to do this as a workaround to a core bug with the config.
     * https://github.com/magento/magento2/issues/3724.
     *
     * @param \Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer $subject
     * @param string[]                                                                 $result
     *
     * @return string[]
     */
    public function afterGetColumns(DefaultRenderer $subject, $result)
    {
        $result['myfield'] = 'col-myfield';

        return $result;
    }
}

etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer">
        <plugin name="vendor_module_sales_plugin_items_myfield_column" type="Vendor\Module\Plugin\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRendererPlugin"/>
    </type>
</config>

Hi,

Have issue like that but its resolved for me changes are taking effect in mail but getting a exception here is my code.

app/code/Vendor/Module/view/frontend/layout/sales_email_order_renderers.xml

<?xml version="1.0"?>
<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.renderers">
            <block class="Magento\Sales\Block\Order\Email\Items\Order\DefaultOrder" as="default" template="Vendor_Module::email/items/order/default.phtml"/>
        </referenceBlock>
    </body>
</page>

Here is my exception :

exception 'Magento\Framework\Exception\LocalizedException' with message 'The element 'sales.email.order.renderers' already has a child with alias 'default'' in public_html/magento21/vendor/magento/framework/Data/Structure.php:611

Not ideal but can be done using a preference and overriding setTemplate.

etc/fontend/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Block\Order\Item\Renderer\DefaultRenderer" type="My\Module\Block\Order\Item\Renderer\DefaultRenderer" />
</config>

Block/Order/Item/Renderer/DefaultRenderer.php

<?php
namespace My\Module\Block\Order\Item\Renderer;

class DefaultRenderer extends \Magento\Sales\Block\Order\Item\Renderer\DefaultRenderer
{
    public function setTemplate($template) {
        return parent::setTemplate('My_Module::order/items/renderer/default.phtml');
    }
}

Having the same issue while overriding default email order template
in file app/code/Vendor/Module/view/frontend/layout/sales_email_order_renderers.xml
` 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.renderers">

        <block class="Magento\Sales\Block\Order\Email\Items\Order\DefaultOrder" as="default" template="Vendor_Module::email/items/order/default.phtml"/>

    </referenceBlock>

</body>

`

Giving exception:
main.CRITICAL: Magento\Framework\Exception\LocalizedException: The element 'sales.email.order.renderers' already has a child with alias 'default' in /var/www/vhosts/shop/vendor/magento/framework/Data/Structure.php:611

and if we remove the alias "as = default" from xml file..
<block class="Magento\Sales\Block\Order\Email\Items\Order\DefaultOrder" name="sales.email.order.renderers.default" template="Vendor_Module::email/items/order/default.phtml"/>

file doesn't get override. Any way to override default template?

@nobuttsalex your method is working just their is small change is directory structure di.xml file should be in Vendor/Module/etc/di.xml instead of Vendor/Module/etc/frontend/di.xml

Here is the code that i use for override default renderer of email template.

Vendor/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Block\Order\Email\Items\Order\DefaultOrder" type="Vendor\Module\Block\Order\Email\Items\Order\DefaultOrder" />
</config>

Vendor/Module/Block/Order/Email/Items/Order/DefaultOrder.php

class DefaultOrder extends \Magento\Sales\Block\Order\Email\Items\Order\DefaultOrder
{
    public function setTemplate($template) {
        return parent::setTemplate('Vendor_Module::email/items/order/default.phtml');
    }
}

For override default.phtml file copy from magento\module-sales\view\frontend\templates\email\items\order\default.phtml
to your module
Vendor/Module/view/frontend/templates/email/items/order/default.phtml

above code worked for me.Thanks.

@shruti-ranium enjoy!

Hi @engcom-backlog-nazar. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if your want to validate it one more time, please, go though the following instruction:

  • [ ] 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.
  • [ ] 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and _stop verification process here_!

  • [ ] 3. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • [ ] 4. If the issue is not relevant or is not reproducible any more, feel free to close it.

hi @forgeonline, The issue was re-tested and we can confirm that it was fixed on the 2.3 release branch. We closing this issue as fixed due to upcoming 2.3 release that will be available soon.

Hello @engcom-backlog-nazar, I have a few questions:

  1. Is this going to be fixed in the 2.2 line as well?
  2. Which commits fix this in the 2.3 branch?

Thanks!

@leoquijano
1 - Yes.
2 - This was fixed by core team

Thanks @engcom-backlog-nazar.

About No. 2, can you or they share the commits here? They are important so we can reproduce the official fix while we wait for the upcoming release.

@forgeonline seem like this commit have partial fix https://github.com/magento/magento2/pull/8353/files

@engcom-backlog-nazar, so it's only partially fixed?

@forgeonline in this commit fix only for 3 points, but on 2.3 release the all point are fixed.

@engcom-backlog-nazar , I'm not the OP, btw :)

Thanks! If you get a chance, can you please let us know if you find the full 2.3 release fix so we can reproduce / backport that one to our setups?

@forgeonline ok, i will try to find this.

In magento 2.3 salable quantity gets reduced.
Admin -> Catalog-> Product -> Get Salable Quantity .

1)https://magento.stackexchange.com/questions/256997/salable-quantity-is-showing-zero-in-product-grid-magento-2/263592#263592
2) https://webkul.com/blog/get-salable-quantity-in-magento-2-3/

Was this page helpful?
0 / 5 - 0 ratings