
Magento 2.x – How to get and access custom attributes on “getLoadedProductCollection()” iteration
If you are modifying Magento_Catalog/templates/product/list.phtml and you are trying to access to a custom attribute <attr_id> inside the loop of
1 |
$collection = $block->getLoadedProductCollection(); |
like this:
1 |
$_product->getData('attr_id') |
but the value is never shown, you should do the following:
- First of all, make sure our custom attribute can be used in product listing: STORES > Product Attributes > select your custom attribute > Storefront Properties > Used in Product Listing to Yes.
- Second, assign our attribute to Attribute Set: STORES > Product Attributes > Attribute Sets.
- Third, make sure that in
vendor/magento/module-catalog/view/frontend/templates/product/list.phtml
, we can see the way and how to get product attribute value.