I need to display a few attributes in the cart page.
How can i accomplish that?
Magento 2 – How to Display Attribute on Cart Page
magento2product-attribute
Related Solutions
No need to create any Observe, you can get it by simple below code.
Just need to create {MODULE_NAME}/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"/>
</group>
</config>
You can get custome attribute in /templates/cart/item/default.phtml
with below code.
echo $_item->getProduct()->getData('custom_attribute');
Try this method
Step1: app/code/Vendor/Module/view/frontend/layout/checkout_index_index.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="checkout" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<referenceBlock name="checkout.root">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="checkout" xsi:type="array">
<item name="children" xsi:type="array">
<item name="sidebar" xsi:type="array">
<item name="children" xsi:type="array">
<item name="summary" xsi:type="array">
<item name="children" xsi:type="array">
<item name="itemsAfter" xsi:type="array">
<item name="children" xsi:type="array">
<item name="gift_message" xsi:type="array">
<item name="component" xsi:type="string">Magento_GiftMessage/js/view/gift-message</item>
<item name="displayArea" xsi:type="string">item_message</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">Magento_GiftMessage/gift-message</item>
<item name="formTemplate" xsi:type="string">Magento_GiftMessage/gift-message-form</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</referenceBlock>
<block class="Magento\GiftMessage\Block\Cart\GiftOptions" name="checkout.order.actions.gift_options" template="Vendor_Module::checkout/gift_options.phtml" cacheable="false" />
</referenceContainer>
</body>
</page>
Step2: app/code/Vendor/Module/view/frontend/templates/checkout/gift_options.phtml
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
?>
<script>
window.giftOptionsConfig = <?= /* @escapeNotVerified */ $block->getGiftOptionsConfigJson() ?>;
</script>
Related Topic
- Magento – Magento 2: Display Custom attribute value under the product name on category page
- Magento 2 – How to Display Custom Product Attributes on Product Page
- Magento – Magento 2 : How to Display Cart Price Rule on Product Page
- Magento 2 – Display Swatches in Product List Page with Add to Cart Functionality
Best Answer
Please follow the below steps:
Assuming you have a module already created
app/code/NameSpace/ModuleName/etc/catalog_attributes.xml
Use like this
<?php echo $_item->getProduct()->getData('discount_price');?>