1

I want to show product custom attribute in sales item ordered section in admin for Bundle product and Configurable product in Magento2.2.0? Refer my screenshot.

enter image description here

0

This is for Configurable product, Crete a Model then in 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="SahiSdi_Invoiceattribute::items/column/name.phtml"/>
    </body>
</page>

then in Template

name.phtml

<?php if ($_item = $block->getItem()): ?>

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('\Magento\Catalog\Model\ProductFactory')->create()->load($_item->getProductId());

$_code = 'custom_attribute ';
$product_type = $product->getTypeId();
if(!empty($product_type) && $product_type == 'configurable'){
  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $simpleProduct = $objectManager->get('Magento\Catalog\Model\Product')->loadByAttribute('sku', $_item->getSku());
  $child_product = $simpleProduct->getResource()->getAttribute($_code)->getFrontend()->getValue($simpleProduct);
    if(!empty($child_product)){
      $_attributeValue = $child_product; 
    }
}else{
  $_attributeValue = $product->getData($_code);
}
?>
    <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 ?>
    <?php if(!empty($_attributeValue )){ ?>
        <dl class="item-options">
            <dt><?= __('Custom Attribute ') ?>:</dt>
            <dd><?php echo $_attributeValue ; ?></dd>
        </dl>
    <?php } ?>
    <?php //Your code ends to show custom attribute value ?>
    <?= $block->escapeHtml($_item->getDescription()) ?>
<?php endif; ?>
| improve this answer | |
  • Thanks Fernandus, its working perfectly fine:) – Amy Dec 2 '18 at 5:14
  • I will try to get them for Bundle – fernandus Dec 2 '18 at 15:21
  • Thanks Fernandus, I will be waiting for your answer. – Amy Dec 6 '18 at 9:25

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.