0

I have overridden phtml files and respective block, i.e. defaultRendere.php file and where I created getCustomAttribute class its return successfully any test data, But I want return Custom Attribute value for that I tried the following Code.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')
                  ->load($this->getItem()->getId());
$test=$product->getData('attribute_name'); 
return $test; 

but its return nothing .. $this->getItem()->getId()) return product id.

2 Answers 2

1

Try to use this code :

protected $_product;

public function __construct(
    ..........
    \Magento\Framework\App\Action\Context $context,
    \Magento\Catalog\Model\Product $product,
    ..........
) {
    parent::__construct($context);
    $this->_product = $product;
}

public function yourFunctionName(){
    $_product = $this->_product->load('your product id');
    $myattribute = $_product->getResource()->getAttribute('Your_Attribute_Code')->getFrontend()->getValue($_product);
    return $myattribute;
}

It may be helpful for you.

1
  • okay! yeah i will try this also , right now its solved by adding "$_item->getProduct()->getData(''my_attribute'');" on default.phtml file Aug 2, 2018 at 11:53
0

Use

     \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory,

or

    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
1
  • okay! yeah i will try this also , right now its solved by adding "$_item->getProduct()->getData(''my_attribute'');" Aug 2, 2018 at 11:53

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.