Getting a custom attribute in the cart should be simple enough: adding a app/code/<Vendor>/<Module>/etc/catalog_attributes.xml
with e.g. the following content:
<?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="sample_attr"/>
</group>
</config>
Should be enough. However, for some reason, when doing
$product = $block->getItem()->getProduct();
$attribute = $product->getCustomAttribute('sample_attr');
in the default.phtml
of the cart, $attribute
will still always be null
, even in developer
mode, or even after cache:flush
and setup:di:compile
. We have also enabled the options Visible on Catalog Pages on Storefront and Used in Product Listing in the attribute's configuration, but still no luck. No matter what we try, the custom attribute is always null
.
This approach works fine for us for the product list (using the catalog_product
group in the catalog_attributes.xml
), but we can't get it working for cart items. Does anyone have any further ideas?
Magento 2.3.2