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
-
Where you want to display? Add screenshot. – Sohel Rana Jan 24 '19 at 16:40
-
check my updated question please – Developper Magento Jan 24 '19 at 16:47
-
in the form.phtml there is no info about product and i just see the _item and the foreach !! can you please give me a solution – Developper Magento Jan 24 '19 at 16:48
-
the screenshot is what i want to have it's just a design – Developper Magento Jan 24 '19 at 16:49
-
This may be of help: magento.stackexchange.com/questions/110531/… – tecjam Jan 24 '19 at 16:57
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.
-
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
-
-
1
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.
-
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
-
2You 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