17

I'm trying to get custom attribut from my product. I tried various methods like below, but non of them worked.

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            /** @var \Magento\Catalog\Model\Product $product */
            $product = $objectManager->get('Magento\Catalog\Model\Product')->load($item->getId());
            $attr = $product->getAttributes();
            $test1 = $product->getData('metal_pads');
            $test2 = $product->getCustomAttribute('metal_pads');
            $test3 = $product->getResource()->getAttribute('metal_pads');
            $test4 = $ddd->getFrontend()->getValue($product);

I'm able to get the Interceptor, but not value.

* edit * I'm using a default value which is also not visible

31

To fetch value of attribute Use:

First get Your Product Collection and then

$this->getProduct()->getAttributeText('attribute_text');

e.g: $this->getProduct()->getAttributeText('metal_pads');

Or

$this->getProduct()->getResource()->getAttributeRawValue($this->getProduct()->getId(),'metal_pads',$this->_storeManager->getStore()->getId());

You can check this question as reference:

How to show the dropdown attibute value using getData() method in magento 2?

Hopefully, It will help you

| improve this answer | |
  • Also var_dump your product collection using get_class_methods(). You will find getMetalPads() methods if your attribute was correctly configured. – Sameer Bhayani Feb 2 '17 at 7:38
  • 1
    perfect answer!!! made my day! +1 :) – SagarPPanchal Sep 12 '18 at 11:09
  • Both are not woking for me. – groy Mar 15 '19 at 12:10
  • In which version you are working? @groy – Sameer Bhayani Mar 15 '19 at 13:43
  • 1
    Was really very helpfull. Thanks – Narendra Vyas May 28 '19 at 13:19
2

You can get the product attribute value like this,

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load('YOUR PRODUCT ID');
echo $product->getMetalPads();
| improve this answer | |
  • alwasy returns null maybe something is wrong with my config.. – xhallix Feb 2 '17 at 6:05
  • verify that you assign that attribute in proper attribute set and that attribute-set assign to your product. – Dhiren Vasoya Feb 2 '17 at 6:14
  • I'm not using a product set, just added a new product attribut and assigned it to the product – xhallix Feb 2 '17 at 6:24
  • Vefiry that you replace proper product_id in above code. Try to get product name, sku. – Dhiren Vasoya Feb 2 '17 at 6:41
  • 8
    Use of ObjectManager is not recommended. – groy Mar 15 '19 at 12:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.