1

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

  • Does it user input? – Sukumar Gorai Nov 20 '18 at 14:30
  • No, the admin adds it from backend – Amy Nov 20 '18 at 15:15
1

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.

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

In admin order section, all item data come from vendor/magento/module-sales/view/adminhtml/templates/order/view/items/renderer/default.phtml core file.

If you want to display any custom attribute below SKU row, then you have to overwrite code file in to you custom module. and put below code.

please overwite default.phtml file in to you custom module and then add below code.

<?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 $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 __("Custom Attribute:") ?></span>
                    <?php echo $_item->getName(); ?>    
                <?php  } ?>
        </td>
    <?php endforeach; ?>
</tr>
  • I override this file. but no changes, but if i change in core file its changing. – Amy Nov 21 '18 at 5:46
  • You have to overwrite admin .phtml file same as below link. You do not modify core file. magento.stackexchange.com/questions/183129/… – Darsh modi Nov 21 '18 at 6:28
  • Hi do you know how to get the attribute value here <?php echo $_item->getName(); ?> – Amy Nov 21 '18 at 10:35
  • please try with $_item->getAttributeText('your_attribute'); – Darsh modi Nov 21 '18 at 10:50
  • If you feel my answer helpful to you then please up vote. :) – Darsh modi Nov 21 '18 at 10:51

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.