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"/>

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

2 Answers 2

  • But why would this be necessary for the cart and not in the product list or the product detail for example? $product->getCustomAttribute('sample_attr') seems work anywhere, but in the cart.
    – fritzmg
    Commented Oct 24, 2019 at 12:29
  • because it it not a custom attribute, you can check into product object with debug. Commented Oct 24, 2019 at 12:30
  • What do you mean by "it is not a custom attribute"? Why would $product->getCustomAttribute('sample_attr') work in the product list or product detail template for example?
    – fritzmg
    Commented Oct 24, 2019 at 12:31
  • ohh, okay then it is customer attribute but if you are trying to fetch it in cart page it will not work because item is not a full product object. you have to load full product object or you can use my code to fetch attribute value without load full object. If it resolved your issue then please accept solution and like it you want. thank you. Commented Oct 24, 2019 at 13:06
  • yes it does not load the full product object, but this is exactly the reson why catalog_attributes.xml exists, and why you can put your custom_attributes into the xml to define that they should be loaded in the product which is stored in the cart item. The Magento2 core also utilizes exactly the same way to add attributes to the quote item.
    – Ekk4rd
    Commented Oct 24, 2019 at 13:17

Following the guide above, and then using:

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

inside the cart item renderer default.phtml works for me... im using Magento2 version 2.3.2.

So the custom attribute not beeing accessible as custom_attribute and only by the resource should not be the issue.

Alternate stuff i did, was to reindex, setup:di:compile and cache:flush.

I would suggest debugging the product attributes by getting them by calling $product->getAttributes() to see how the attribute is stored inside your cart item object.

  • $product->getAttributes() lists the custom attribute, but its content is null.
    – fritzmg
    Commented Oct 24, 2019 at 12:57
  • that was the problem i had, while my catalog_attributes.xml was invalid... maybe it is an issue with that, but in the code snippet you posted here i dont see anything what would be wrong. Maybe your module isn't loaded or something like this ?
    – Ekk4rd
    Commented Oct 24, 2019 at 13:10
  • In actuality, there are way more attributes already defined in our catalog_attributes.xml. And they all work fine for the product list (using the catalog_product group). The XML used for the quote_item group is otherwise exactly as I posted in my original post. I double and triple checked it too :). If there was a syntax error, presumably Magento would tell me, especially in the developer mode.
    – fritzmg
    Commented Oct 24, 2019 at 14:34
  • Weirdly though, it does work when using $product->getData('sample_attr'). Now I am thoroughly confused.
    – fritzmg
    Commented Oct 24, 2019 at 14:35
  • maybe magento interprets your attribute as default_attribute instead of custom_attribute ? Now im really interested in how magento differs between default and custom attributes...
    – Ekk4rd
    Commented Oct 24, 2019 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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