I am trying to display a custom product attribute under the order summary list of items on checkout. I think it is fed by the TotalsItemExtensionInterface
. I have the following trying to add the custom attribute to the collection exposed on the front end.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Quote\Api\Data\TotalsItemExtensionInterface">
<attribute code="product" type="string">
<join reference_table="catalog_product" reference_field="product_id" join_on_field="entity_id">
<field>custom_attribute</field>
</join>
</attribute>
</extension_attributes>
</config>
Any ideas on how this is done to attach a product attribute to a quote item in Magento 2?
EDIT: Attached is the screenshot of where I would like to get the attribute to show:
Also I am taking a different route, but have the same problem. I brought the attribute to the quote item through a migration so is now part of the quote_item
table. But not part of the collection coming through. Any idea on how I get that attribute as part of the quoteItem
object?
Best Answer
1) The flow of cart summary in checkout page
We can get the
totalsData
from:vendor/magento/module-checkout/view/frontend/web/js/model/quote.js
If we try to format the Json from
window.checkoutConfig
, we can see thetotalsData
. And, we can get the items from it.Now, at look the checkout xml layout:
vendor/magento/module-checkout/view/frontend/layout/checkout_index_index.xml
In the front page:
2) Add custom attribute
Where is the
totalsData
from the PHP side?vendor/magento/module-checkout/Model/DefaultConfigProvider.php
So, we need to override this method, I think we should use Plugin to override this method.Vendor\Module\etc\di.xml
Vendor\Module\Model\Checkout\ConfigProviderPlugin.php
And then, we need to create your own js and html template files or override the exist files.
[EDIT]
We should try with:
Create extension attributes:
app/code/Vendor/Module/etc/extension_attributes.xml
app/code/Vendor/Module/etc/di.xml
app/code/Vendor/Module/Plugin/Cart/Totals/ItemConverterPlugin.php
On the checkout page, we can check on Browser console:
window.checkoutConfig.totalsData.items