2

getResource() is deprecated so my question is what do I need to use instead to be future proof for next updates.

I use this function like below in the catalog list.phtml

$seedBank = $_product->getResource()->getAttribute('brand')->getFrontend()->getValue($_product);
2

You can get product custom attribute option label directly as below:

$_product->getAttributeText('brand');

This will return label of product selected brand.

  • 1
    Awesome, that worked like a charm ! – Riccardo Dec 2 at 11:48
2

Have an instance of \Magento\Catalog\Mode\ResourceModel\Product as a dependency for your class and call methods from that class.

/**
 * @var \Magento\Catalog\Mode\ResourceModel\Product
 */

private $productResource;

public function __construct(
    ....
    \Magento\Catalog\Mode\ResourceModel\Product $productResource,
    ....
) {
    ....
    $this->productResource = $productResource;
    ....
}

now, in the methods where you need the value, let's say $_product is the product instance you can do this:

$seedBank = $this->productResource()->getAttribute('brand')->getFrontend()->getValue($_product);
  • 1
    Thanks, this will work on other pages. On the Category page the following will be enough: $_product->getAttributeText('brand'); – Riccardo Dec 2 at 11:51

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.