0

I have added 2 custom attributes to product. I want to display them in the cart so how to proceed to get them in the cart page because i can't understand this form.phtml how it works?? I need a help please enter image description here

0

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.

| improve this answer | |
  • thanks a lot just one qquestion i can't see the attribute if i added from configurable product why ??* – Developper Magento Jan 25 '19 at 8:49
  • 1
    $_product = $block->getProduct(); $myattribute = $_product->getResource()->getAttribute('Your_Attribute_Code')->getFrontend()->getValue($_product); echo $myattribute; – Rakesh Donga Jan 25 '19 at 8:53
  • ok so istead of <?php echo $_item->getProduct()->getData('discount_price');?> i have to put this $_product = $block->getProduct(); $myattribute = $_product->getResource()->getAttribute('Your_Attribute_Code')->getFrontend()->getValue($_product); echo $myattribute; – Developper Magento Jan 25 '19 at 8:56
  • this code in default.phtml ?? – Developper Magento Jan 25 '19 at 9:20
  • 1
    yes @DevelopperMagento you are right – Rakesh Donga Jan 25 '19 at 9:22
3

You can add attribute in form as hidden text box. For example attribute color and value is red then you can add In this way you can add your all attributes on product form. When it is submitted all information is stored in quote object with name additionalOptions(not sure about attribute name but you can check for it). On cart page you can get those information and use them according to your requirement. I did here https://shingora.net/savannah-11535.html you can get some idea.

I found almost same post as per your requirement : How To Add Additional Options In Magento 2 Cart

Please check once and let me know if it helps you.

| improve this answer | |
  • I want to make a script so when i change the qty i will have to calculate the bnew qty multiplied in some attributes where to put this script please in default.xml – Developper Magento Jan 25 '19 at 8:45
  • 2
    You can put script in view.phtml file for product view and can create a input field as i explained in above answer. – kushal Jan 27 '19 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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