3

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? – chirag Nov 16 '16 at 8:21
  • you can have attribute set of the product, you can get all attributes of that attribute set. – Ronak Chauhan Nov 16 '16 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. – Nitin Pawar Nov 16 '16 at 8:57
  • If you have product object then you have all its data, so just use foreach() loop and print data in phtml – Ronak Chauhan Nov 16 '16 at 9:02
  • And if you want a attribute information then just use attribute set id and get all its attribute – Ronak Chauhan Nov 16 '16 at 9:03
5

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.

$id = YOUR_PRODUCT_ID;
$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 />';    
}
3

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

$product->getResource()->getAttribute($code)->getFrontend()->getValue($product); 

May it help to some one, Thanks.

0

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();
    var_dump($data);

    // 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
    var_dump($this->_objectManager->create('Magento\Catalog\Model\Product')->load($id)->getAttributeText('test'));
  • Use product factory to load the product with DI : \Magento\Catalog\Model\ProductFactory : $productFactory->create()->load($id); – Franck Garnier Nov 16 '16 at 9:58

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.