Magento – Magento 2 Credit Memos : Template override doesn’t work after clicking “Update Qty’s” button

creditmemomagento-2.1moduletemplate

I am creating a custom module to simplify the Credit Memo creation for admin users. One of the features is hiding the "Return to Stock" column. I have overwritten 2 files for this:

vendor/magento/module-sales/view/adminhtml/templates/order/creditmemo/create/items.phtml

vendor/magento/module-sales/view/adminhtml/templates/order/creditmemo/create/items/renderer/default.phtml

With the following files of my custom module:

app/code/ATC/CreditMemo/view/adminhtml/layout/sales_order_creditmemo_new.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">
    <body>
        <referenceBlock name="order_items">
            <action method="setTemplate">
                <argument name="template" translate="true" xsi:type="string">ATC_CreditMemo::order/creditmemo/create/items.phtml</argument>
            </action>
            <block class="Magento\Sales\Block\Adminhtml\Items\Renderer\DefaultRenderer" as="default" template="ATC_CreditMemo::order/creditmemo/create/items/renderer/default.phtml"/>
        </referenceBlock>
    </body>
</page>

app/code/ATC/CreditMemo/view/adminhtml/templates/order/creditmemo/create/items.phtml

In this file I simply removed the following code:

<?php if ($block->canReturnToStock()) : ?>
<th class="col-return-to-stock"><span><?php /* @escapeNotVerified */ echo __('Return to Stock') ?></span></th>
<?php endif; ?>

app/code/ATC/CreditMemo/view/adminhtml/templates/order/creditmemo/create/items/renderer/default.phtml

In this file I simply removed the following code:

<?php if ($block->canParentReturnToStock($_item)) : ?>
    <td class="col-return-to-stock">
    <?php if ($block->canReturnItemToStock($_item)) : ?>
        <input type="checkbox"
               class="admin__control-checkbox"
               name="creditmemo[items][<?php /* @escapeNotVerified */ echo $_item->getOrderItemId() ?>][back_to_stock]"
               value="1"<?php if ($_item->getBackToStock()):?> checked<?php endif;?>/>
        <label class="admin__field-label"></label>
    <?php endif; ?>
    </td>
<?php endif; ?>

All of this works fine when creating a new Credit Memo until a user clicks
the "Update Qty's" button after changing the value of the "Qty to Refund" of any order item. When clicking this button, the following Javascript function is triggered:

submitAndReloadArea($('creditmemo_item_container'),'http://store_url.com/admin/sales/order_creditmemo/updateQty/order_id/xxxx/key/yyyy/')

The whole "#creditmemo_item_container" HTML area gets reloaded and the "Return to Stock" column reappears. It seems like the template files I have overwritten are still being used when calling that function. How could I fix this so the template files of my custom module are used instead (just like the first time the page is loaded)?

Best Answer

First of all, you need to correct: app/code/ATC/CreditMemo/view/adminhtml/layout/sales_order_creditmemo_new.xml

for proper overwrite renderer/default.phtml so change:

<block class="Magento\Sales\Block\Adminhtml\Items\Renderer\DefaultRenderer" as="default" template="ATC_CreditMemo::order/creditmemo/create/items/renderer/default.phtml"/>

to:

<referenceBlock name="order_items.default">
        <action method="setTemplate">
            <argument name="template" translate="true" xsi:type="string">ATC_CreditMemo::order/creditmemo/create/items/renderer/default.phtml</argument>
        </action>
</referenceBlock>

And the reason why after update qty the block is refreshed, because you miss overwriting sales_order_creditmemo_updateqty.xml

Create app/code/ATC/CreditMemo/view/adminhtml/layout/sales_order_creditmemo_updateqty.xml

and paste the same content after fix as you have in sales_order_creditmemo_new.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">
    <body>
    <referenceBlock name="order_items">
        <action method="setTemplate">
            <argument name="template" translate="true" xsi:type="string">ATC_CreditMemo::order/creditmemo/create/items.phtml</argument>
        </action>
    </referenceBlock>
    <referenceBlock name="order_items.default">
        <action method="setTemplate">
            <argument name="template" translate="true" xsi:type="string">ATC_CreditMemo::order/creditmemo/create/items/renderer/default.phtml</argument>
        </action>
    </referenceBlock>
</body>

Related Topic