2

I want to show Custom Attribute in items invoiced below the SKU in magento2.2.0?

Please refer below screenshot.

enter image description here

13
  • What is the attribute value you want to show? Also does this value saved somewhere in order item table or it should come from Product? Nov 26 '18 at 13:25
  • The custom attribute is created from backend. its value is 5
    – Amy
    Nov 26 '18 at 13:27
  • Do you want to show this only on invoice items? Nov 26 '18 at 13:40
  • Please check my answer that will only work for invoice view page. Nov 26 '18 at 14:01
  • Hi, I have tried your code. its working perfectly fine. But i want the same logic on Item orders Table, Items to Refund, Invoice History Email, Email Copy of Credit Memo. Thanks:)
    – Amy
    Nov 27 '18 at 7:16
1

Create a module with name STech_Invoiceattribute by following below steps:

Step 1: Create registration.php under

app/code/STech/Invoiceattribute/registration.php

<?php

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

Step 2: Create module.xml under

app/code/STech/Invoiceattribute/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_Invoiceattribute" setup_version="0.0.1">
        <sequence>
            <module name="Magento_Sales"/>
            <module name="Magento_Quote"/>
            <module name="Magento_Checkout"/>
        </sequence>
    </module>
</config>

Step 3: Create sales_order_view.xml under

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

Step 4: Create sales_order_invoice_view.xml under

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

Step 5: Create adminhtml_order_shipment_view.xml under

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

Step 6: Create sales_order_creditmemo_view.xml under

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

Step 7: Create name.phtml under

app/code/STech/Invoiceattribute/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".

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.