cancel
Showing results for 
Search instead for 
Did you mean: 

Get product attribute in cart in Magento2.2

SOLVED

Get product attribute in cart in Magento2.2

I am migrating Magento 1.9 to Magento 2.2 and I need to know how to display the Delivery Term attribute that each product has in the cart. Someone could help me? because I've tried several options but I can not get any of them to work for me.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Get product attribute in cart in Magento2.2

There is new concept in Magento 2 to create catalog_attributes.xml file in which you can define your custom attribute value using quote_item group section.

 

You Just need to create app/code/Vendor/Modulename/etc/catalog_attributes.xml with below 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="custom_attribute_name"/>
    </group>
</config>

You can get custom_attribute_name in the template file withcart/item/default.phtml below code.

echo $_item->getProduct()->getData('custom_attribute_name');

 Clear Cache and check.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

View solution in original post

7 REPLIES 7

Re: Get product attribute in cart in Magento2.2

There is new concept in Magento 2 to create catalog_attributes.xml file in which you can define your custom attribute value using quote_item group section.

 

You Just need to create app/code/Vendor/Modulename/etc/catalog_attributes.xml with below 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="custom_attribute_name"/>
    </group>
</config>

You can get custom_attribute_name in the template file withcart/item/default.phtml below code.

echo $_item->getProduct()->getData('custom_attribute_name');

 Clear Cache and check.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: Get product attribute in cart in Magento2.2

If i do it with the attribute SKU it shows me the value well, but if I do it with the attribute tiempo_envio that is defined for each product and indicates the delivery time, it does not show me anything.

Re: Get product attribute in cart in Magento2.2

Thank so much. It´s ok now Smiley Happy 

Re: Get product attribute in cart in Magento2.2

I hope you have resolved your issue.
You can accept this answer so other user in community have useful in future
If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: Get product attribute in cart in Magento2.2

You can also use

 

echo $_item->getProduct()->getCustomAttributeName();

Re: Get product attribute in cart in Magento2.2

Thank you so much, it works in my website!

 

Web Design

Re: Get product attribute in cart in Magento2.2

Its giving the Error! 

Exception #0 (Exception): Notice: Undefined variable: _item in