23

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

0
42

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

$product = $this->getProduct();
$ressource = $product->getResource();
$store = $this->_storeManager->getStore();

$ressource->getAttributeRawValue($product->getId(),'metal_pads',$store->getId());

You can check this question as reference:

How to show the drop down attribute value using getData() method in magento 2?

Hopefully, It will help you

8
  • 1
    Also var_dump your product collection using get_class_methods(). You will find getMetalPads() methods if your attribute was correctly configured. Feb 2 '17 at 7:38
  • Both are not woking for me.
    – groy
    Mar 15 '19 at 12:10
  • In which version you are working? @groy Mar 15 '19 at 13:43
  • Glad to know it :) sorry for late @SagarPPanchal Mar 15 '19 at 13:44
  • I am using Magento 2.2.5. However magento.stackexchange.com/questions/231033/… fixed issue.
    – groy
    Mar 15 '19 at 14:12
3

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();
7
  • 1
    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. 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. Feb 2 '17 at 6:41
  • 9
    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.