3

How to show product custom attribute with values in items ordered section in magento2.2.0? Refer screenshotenter image description here

The above image is my custom attribute(PCB Master)enter image description here

In the above image i need to show my custom attribute with its value under SKU in magento2.2.0

2
  • Does it user input? Commented Nov 20, 2018 at 14:30
  • No, the admin adds it from backend
    – Amy
    Commented Nov 20, 2018 at 15:15

1 Answer 1

5

To overwrite any adminhtml template file you need to create custom module. For you order item section changes I have overwrite vendor/magento/module-sales/view/adminhtml/templates/order/view/items/renderer/default.phtml in to my module.

First of all create etc/module.xml file. Below is the code for module.xml file.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Darsh_Orderitem" setup_version="1.0.0" >      
    </module>
</config>

Create view/adminhtml/layout/sales_order_view.xml file in your module. In this file I have used "referenceBlock" tag to overwrite core template file and used "setTemplate" method.

<?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" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">    
    <body>
        <referenceBlock name="default_order_items_renderer">
            <action method="setTemplate">
                <argument name="template" translate="true" xsi:type="string">Darsh_Orderitem::order/view/items/renderer/default.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

Now, create phtml file in your folder view/adminhtml/templates/order/view/items/renderer/default.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php /** @var \Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer $block */ ?>
<?php $_item = $block->getItem() ?>
<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('\Magento\Catalog\Model\ProductFactory')->create()->load($_item->getProductId());
?>
<?php $block->setPriceDataObject($_item) ?>
<tr>
    <?php $i = 0;
    $columns = $block->getColumns();
    $lastItemNumber = count($columns) ?>
    <?php foreach ($columns as $columnName => $columnClass):?>
        <?php $i++; ?>
        <td class="<?= /* @noEscape */ $columnClass ?><?= /* @noEscape */ ($i === $lastItemNumber ? ' last' : '') ?>"><?= /* @escapeNotVerified */ $block->getColumnHtml($_item, $columnName) ?>
            <?php if($columnClass == "col-product"){  ?>
                <span><?php echo __("PCB:") ?></span>
                    <?php echo $product->getData('pcb_master');; ?> 
                <?php  } ?>
        </td>
    <?php endforeach; ?>
</tr>

In phtml file I have load product data and get "pcb_master" attribute value.

If you still you are facing issue then you can download module from https://github.com/Darshanmodi1427/Orderitem

Thanks.

9
  • Hi Amy, Is my answer helpful to you or still facing issue? If it is helpful then please up vote. Commented Nov 23, 2018 at 3:59
  • Hi Darsh, yesterday i was on leave so i am checking now. i will let you know ASAP. Thanks:)
    – Amy
    Commented Nov 23, 2018 at 5:24
  • Hi Darsh i have applied your module. but nothing is shown. Is there any problem creating the module
    – Amy
    Commented Nov 23, 2018 at 5:42
  • You have download module from Git ? I have checked it was working fine. github.com/Darshanmodi1427/Orderitem Commented Nov 23, 2018 at 5:52
  • Yes Darsh, i have downloaded from your link only. But i am not able to identify what's the mistake.
    – Amy
    Commented Nov 23, 2018 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.