24

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

2 Answers 2

47

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

9
  • 1
    Also var_dump your product collection using get_class_methods(). You will find getMetalPads() methods if your attribute was correctly configured. Commented Feb 2, 2017 at 7:38
  • Both are not woking for me.
    – groy
    Commented Mar 15, 2019 at 12:10
  • In which version you are working? @groy Commented Mar 15, 2019 at 13:43
  • Glad to know it :) sorry for late @SagarPPanchal Commented Mar 15, 2019 at 13:44
  • 1
    Beware that this does retrieve the translated value, so if you're building logic on certain values you may have to keep that in mind
    – Jacques
    Commented Aug 21, 2023 at 7:29
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();

NOTE : Use of Objectmanager is not recommended, so you can use it by Block or Helper.

8
  • 1
    alwasy returns null maybe something is wrong with my config..
    – xhallix
    Commented Feb 2, 2017 at 6:05
  • verify that you assign that attribute in proper attribute set and that attribute-set assign to your product. Commented Feb 2, 2017 at 6:14
  • I'm not using a product set, just added a new product attribut and assigned it to the product
    – xhallix
    Commented Feb 2, 2017 at 6:24
  • Vefiry that you replace proper product_id in above code. Try to get product name, sku. Commented Feb 2, 2017 at 6:41
  • 12
    Use of ObjectManager is not recommended.
    – groy
    Commented Mar 15, 2019 at 12:11

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