How to show Product Custom Attribute in edit page of Credit Memos, Invoice and Shipments below SKU in admin in Magento2.2.0?

magento.stackexchange https://magento.stackexchange.com/questions/251558

  •  06-02-2021
  •  | 
  •  

Question

I want to show Product Custom Attribute in edit page of Invoice, Shipments and Credit Memos below SKU in admin in Magento2.2.0? Refer my Screenshot.

enter image description here

enter image description here

enter image description here

Any help would be appreciated.

Was it helpful?

Solution

Create a module with name STech_Salesattribute by following below steps:

Step 1: Create registration.php under

app/code/STech/Salesattribute/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'STech_Salesattribute',
    __DIR__
);

Step 2: Create module.xml under

app/code/STech/Salesattribute/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="STech_Salesattribute" setup_version="0.0.1">
        <sequence>
            <module name="Magento_Sales"/>
            <module name="Magento_Quote"/>
            <module name="Magento_Checkout"/>
        </sequence>
    </module>
</config>

Step 3: Create adminhtml_order_shipment_new.xml under

app/code/STech/Salesattribute/view/adminhtml/layout/adminhtml_order_shipment_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="column_name" template="STech_Salesattribute::items/column/name.phtml"/>
    </body>
</page>

Step 4: Create sales_order_creditmemo_new.xml under

app/code/STech/Salesattribute/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="column_name" template="STech_Salesattribute::items/column/name.phtml"/>
    </body>
</page>

Step 5: Create sales_order_invoice_new.xml under

app/code/STech/Salesattribute/view/adminhtml/layout/sales_order_invoice_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="column_name" template="STech_Salesattribute::items/column/name.phtml"/>
    </body>
</page>

Step 6: Create name.phtml under

app/code/STech/Salesattribute/view/adminhtml/templates/items/column/name.phtml

<?php if ($_item = $block->getItem()): ?>
    <div id="order_item_<?= /* @escapeNotVerified */ $_item->getId() ?>_title"
         class="product-title">
        <?= $block->escapeHtml($_item->getName()) ?>
    </div>

    <div class="product-sku-block">
        <span><?= /* @escapeNotVerified */ __('SKU') ?>:</span> <?= implode('<br />', $this->helper('Magento\Catalog\Helper\Data')->splitSku($block->escapeHtml($block->getSku()))) ?>
    </div>

    <?php if ($block->getOrderOptions()): ?>
        <dl class="item-options">
            <?php foreach ($block->getOrderOptions() as $_option): ?>
                <dt><?= /* @escapeNotVerified */ $_option['label'] ?>:</dt>
                <dd>
                    <?php if (isset($_option['custom_view']) && $_option['custom_view']): ?>
                        <?= /* @escapeNotVerified */ $block->getCustomizedOptionValue($_option) ?>
                    <?php else: ?>
                        <?php $_option = $block->getFormattedOption($_option['value']); ?>
                        <?= /* @escapeNotVerified */ $_option['value'] ?><?php if (isset($_option['remainder']) && $_option['remainder']): ?><span id="<?= /* @escapeNotVerified */ $_dots = 'dots' . uniqid() ?>"> ...</span><span id="<?= /* @escapeNotVerified */ $_id = 'id' . uniqid() ?>"><?= /* @escapeNotVerified */ $_option['remainder'] ?></span>
                            <script>
                                require(['prototype'], function() {
                                    $('<?= /* @escapeNotVerified */ $_id ?>').hide();
                                    $('<?= /* @escapeNotVerified */ $_id ?>').up().observe('mouseover', function(){$('<?= /* @escapeNotVerified */ $_id ?>').show();});
                                    $('<?= /* @escapeNotVerified */ $_id ?>').up().observe('mouseover', function(){$('<?= /* @escapeNotVerified */ $_dots ?>').hide();});
                                    $('<?= /* @escapeNotVerified */ $_id ?>').up().observe('mouseout',  function(){$('<?= /* @escapeNotVerified */ $_id ?>').hide();});
                                    $('<?= /* @escapeNotVerified */ $_id ?>').up().observe('mouseout',  function(){$('<?= /* @escapeNotVerified */ $_dots ?>').show();});
                                });
                            </script>
                        <?php endif; ?>
                    <?php endif; ?>
                </dd>
            <?php endforeach; ?>
        </dl>
    <?php endif; ?>
    <?php //Your code starts to show custom attribute value ?>
    <dl class="item-options">
        <dt><?= __('Custom Label') ?>:</dt>
        <dd><?= $_item->getProduct()->getCustom() ?></dd>
    </dl>
    <?php //Your code ends to show custom attribute value ?>
    <?= $block->escapeHtml($_item->getDescription()) ?>
<?php endif; ?>

Thats it!. Now run setup upgrade and other required commands and check. Don't forget to replace your attribute code in place of "custom".

OTHER TIPS

Here is the example of override invoice file in a custom module

Create sales_order_invoice_new, sales_order_invoice_updateqty, sales_order_invoice_view

app/code/Vendor/Module/view/adminhtml/layout/sales_order_invoice_view.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.default">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Vendor_Module::order/invoice/create/items/renderer/default.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

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

to

app/code/Vendor/Module/view//adminhtml/templates/order/invoice/create/items/renderer/default.phtml

Here in default.phtml you need to load full product object by product id $_item->getProductId() and get custom attribute value

<?php $_item = $block->getItem() ?>
<?php $block->setPriceDataObject($_item)?>
    <td class="col-product"><?= $block->getColumnHtml($_item, 'name') ?></td>
    <?php
    // Load product by $_item->getProductId() and get custom attribute value here */
    ?>
    <td class="col-price">
        <?= $block->getColumnHtml($_item, 'price') ?>
    </td>

For creditmemo override

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

For shipment override

vendor/magento/module-sales/view/adminhtml/templates/items/column/name.phtml

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top