13

When I try to get data from the material attribute like $_product->getData('material'), it shows the integer value. How can I convert it to the actual front-end value and in which file do I have to make the changes?.

5 Answers 5

18

Instead of getData() you need to use getAttributeText('attribute_code') for dropdown attributes. like

$_product->getAttributeText('material');

For multi-select try this :

$result = $_product->getResource()->getAttribute('material')->getFrontend()->getValue($_product);
5
  • material has multi select values and it gives the values like 25,19,15 on 'getData()' where as getAttributeText('material') gives blank page on load
    – user47136
    Nov 17, 2016 at 7:31
  • @Hemant, Check my updated answer once and let me know. Nov 17, 2016 at 9:03
  • 1
    product->getResource() seems to be deprecated
    – Alex
    Mar 13, 2019 at 11:15
  • Also, we should make sure that the custom attribute is set to be visible in its attribute property.
    – Thowzif
    Jan 1, 2020 at 13:18
  • 1
    Hello, I'm trying to get the "barcode" attribute but the result is "null", where is the mistake? $product->getAttributeText('barcode')
    – Silver
    Jan 15, 2020 at 13:00
8

To fetch value of attribute Use:

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

Or

$this->getProduct()->getResource()->getAttributeRawValue($this->getProduct()->getId(),'material',$this->_storeManager->getStore()->getId());
2
  • 1
    product->getResource() seems to be deprecated
    – Alex
    Mar 13, 2019 at 11:15
  • $this->getProduct()->getResource()->getAttributeRawValue($this->getProduct()->getId(),'material',$this->_storeManager->getStore()->getId()); works on v2.3.4-p1
    – Asrar
    Apr 22, 2020 at 11:30
8

You can get product attribute option values by below code

$_product->getAttributeText('material');

You can get multi select option values by

$_attributeValue = $block->getProduct()->getResource()->getAttribute('sizing_guidelines')->getFrontend()->getValue($block->getProduct());
7
  • Sorry material has multi select values and it gives the values like 25,19,15
    – user47136
    Nov 17, 2016 at 7:31
  • Try my updates answer Nov 17, 2016 at 7:32
  • I tried it gives blank page
    – user47136
    Nov 17, 2016 at 7:33
  • Have you got any solution for that and tell me which file in try to it. Nov 17, 2016 at 7:34
  • i Am trying to get data in phtml template
    – user47136
    Nov 17, 2016 at 7:35
3
$_product->getAttributeText('material');

is the right answer --- beware that $_product->getResource() is a deprecated function

1

The answers above were partially missing something (at least for my case, since I needed to use the custom attribute value inside the cart template), if you want to pass the custom attribute to a template proceed like this:

Create File catalog_attributes.xml inside /etc folder of any existing module

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
    <group name="quote_item">
     <attribute name="insert_here_your_attribute_code"/>
    </group>
</config>

and from inside the template, in case of attribute of type select

<?php /* @escapeNotVerified */ echo $product->getAttributeText('attribute_code') ?>

where

$product = $block->getItem()->getProduct()

$block in this case was \Magento\Checkout\Block\Cart\Item\Renderer

In case of attribute of type text instead

<?php /* @escapeNotVerified */ echo $product->getCustomAttribute('attribute_code')->getValue() ?>

after that just execute

bin/magento c:c

And you should see the value of the custom attribute in your cart page or any other template files.

1
  • this is works to call catalog custom attribute value from quote item. thanks
    – mrfizh
    Feb 7 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy