2

How to show product attribute value in order detail in Magento 2.2.6? I already tried below code but not working:

File Path:

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

$_item->getProduct()->getAttributeText('attribute_code');
$_item->getProduct()->getProductAttribute('attribute_code');

Need Help.

enter image description here

2

You can get product attribute value using $objectmanager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productId = $_item->getProduct()->getId();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
echo $product->getData('attribute_code');

You can also try this code as well ( For select option attribute )

echo $product->getResource()->getAttribute('attribute_code')->getFrontend()->->getValue($product);
  • How to check if attribute value is not empty then echo? – Shoaib Saleem Jan 25 at 9:19
  • Above code not working. – Shoaib Saleem Jan 25 at 9:37
  • Please check my answer. – Shoaib Saleem Jan 25 at 9:59
  • Is this code snippet also work for front end view or just admin? – Shoaib Saleem Jan 25 at 10:32
  • Tried same code but it break site front end. – Shoaib Saleem Jan 25 at 10:48
0

@Aasim answer works.I added below code to get attribute value.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $productId = $_item->getProduct()->getId();
            $product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
            $value = $product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($product);
            if ($value) {
                echo '<p><strong>'.$value.'</strong></p>';
            } 
  • Please accept my ans and upvote👍 if its useful to you!! thanks :) – Aasim Goriya Jan 30 at 9:17

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.