Magento 2 – How to Display Attribute on Cart Page

magento2product-attribute

I need to display a few attributes in the cart page.
How can i accomplish that?
enter image description here

Best Answer

Please follow the below steps:

Assuming you have a module already created

Step 1: Create an attribute and assign to product Ex: discount_price (your product attribute). Call the same from your custom module as shown below:

app/code/NameSpace/ModuleName/etc/catalog_attributes.xml

<?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="discount_price"/>
    </group>
</config>

Step 2: Go to below path: app/design/frontend/[themepackage]/[theme]/Magento_Checkout/templates/cart/item/default.phtml

Use like this <?php echo $_item->getProduct()->getData('discount_price');?>

Step 3: Clear cache and page cache and go to frontend & check the same.