1

How do I add custom attribute "color". I have made to order items in backend.

I have tried:

  • $_item->getColor()
  • $_item->getAttributes()
  • $_item->getAttribute('color')

And a bunch of other stuff:

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

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

// @codingStandardsIgnoreFile

?>
<?php
/**
 * @see \Magento\Sales\Block\Adminhtml\Items\Column\Name
 */
?>

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

    <div class="product-sku-block">
        <span><?php /* @escapeNotVerified */ echo __('SKU') ?>:</span> <?php echo 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><?php /* @escapeNotVerified */ echo $_option['label'] ?>:</dt>
                <dd>
                    <?php if (isset($_option['custom_view']) && $_option['custom_view']): ?>
                        <?php /* @escapeNotVerified */ echo $block->getCustomizedOptionValue($_option); ?>
                    <?php else: ?>
                        <?php $_option = $block->getFormattedOption($_option['value']); ?>
                        <?php /* @escapeNotVerified */ echo $_option['value']; ?><?php if (isset($_option['remainder']) && $_option['remainder']): ?><span id="<?php /* @escapeNotVerified */ echo $_dots = 'dots' . uniqid()?>"> ...</span><span id="<?php /* @escapeNotVerified */ echo $_id = 'id' . uniqid()?>"><?php /* @escapeNotVerified */ echo $_option['remainder'] ?></span>
                            <script>
                                require(['prototype'], function() {
                                    $('<?php /* @escapeNotVerified */ echo $_id ?>').hide();
                                    $('<?php /* @escapeNotVerified */ echo $_id ?>').up().observe('mouseover', function(){$('<?php /* @escapeNotVerified */ echo $_id ?>').show();});
                                    $('<?php /* @escapeNotVerified */ echo $_id ?>').up().observe('mouseover', function(){$('<?php /* @escapeNotVerified */ echo $_dots?>').hide();});
                                    $('<?php /* @escapeNotVerified */ echo $_id ?>').up().observe('mouseout',  function(){$('<?php /* @escapeNotVerified */ echo $_id ?>').hide();});
                                    $('<?php /* @escapeNotVerified */ echo $_id ?>').up().observe('mouseout',  function(){$('<?php /* @escapeNotVerified */ echo $_dots ?>').show();});
                                });
                            </script>
                        <?php endif; ?>
                    <?php endif; ?>
                </dd>
            <?php endforeach; ?>
        </dl>
    <?php endif; ?>
    <?php echo $block->escapeHtml($_item->getDescription()) ?>
<?php endif; ?>
3

Item object has a product property that you can retrieve by

       `$product = $_item->getProduct();
        $color = $product->getData('color');

Make sure that $_item is instance of Magento\Sales\Model\Order\Item

  • As a one-liner: $_item->getProduct()->getData('color'); – Cudos May 14 '17 at 14:42
0

Try this

$objectManager = Magento\Framework\App\ObjectManager::getInstance();

$product = $objectManager->create('Magento\Catalog\Model\Product')->load($_item->getProductId());

$color = $product->getAttributeText('color');
  • 2
    Do not use object manager directly in your code. instead of this use DI – Rohit Kundale May 13 '17 at 6:11
0

It worked for me. for get value from drop-down attribute:

$color = $product->getAttributeText('color');

instead of:

$color = $product->getData('color'); 

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.