Hi i want to show the products attribute code and it's value using the product id.

How can i do this? Any help would be appreciated.

  • are you trying to get all product attributes of that product id? Nov 16, 2016 at 8:21
  • you can have attribute set of the product, you can get all attributes of that attribute set. Nov 16, 2016 at 8:30
  • @RonakChauhan Can you please explain me how can i get it from the product id. I want to get the attributes with it's value in my template file. Nov 16, 2016 at 8:57
  • If you have product object then you have all its data, so just use foreach() loop and print data in phtml Nov 16, 2016 at 9:02
  • And if you want a attribute information then just use attribute set id and get all its attribute Nov 16, 2016 at 9:03

3 Answers 3


You can use this,

namespace NameSpace\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
protected $_productRepository;

public function __construct(
    \Magento\Backend\Block\Template\Context $context,        
    \Magento\Catalog\Model\ProductRepository $productRepository,
    array $data = []
    $this->_productRepository = $productRepository;
    parent::__construct($context, $data);

public function getProductById($id)
    return $this->_productRepository->getById($id);

public function getProductBySku($sku)
    return $this->_productRepository->get($sku);

and in your phtml.

$product = $block->getProductById($id); // load product bi ID
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) { 
   echo $attribute->getAttributeCode(); echo '<br />';
   echo $attribute->getStoreLabel(); echo '<br />';
   echo $attribute->getFrontendLabel(); echo '<br />';    
   echo $attribute->getFrontend()->getLabel(); echo '<br />';

// you might not get value for all attributes
   echo $attribute->getFrontend()->getValue($product); echo '<br />';    

All other answer are helpful but i want to get the attribute value with multi select also. so i used the


May it help to some one, Thanks.


it depends on the attribute type.

 //to get all the attributes of the product by id
    $id = 1; //your Product id
    $data = $this->_objectManager->create('Magento\Catalog\Model\Product')->load($id)->getData();

    // also you can get the attribute value dpendong on the attribute type
    //for example you have text field Attribute called(test) to get it's value
    echo  $this->_objectManager->create('Magento\Catalog\Model\Product')->load($id)->getTest();

    //to get a value of select/multi-select Attribute called(test) will return as array
  • Use product factory to load the product with DI : \Magento\Catalog\Model\ProductFactory : $productFactory->create()->load($id); Nov 16, 2016 at 9:58

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.