I've overridden the grand-total
element in checkout_cart_index
layout and in my custom component JS I need to get the data that I have in a PHP class with a specific logic.
How can I get the data returned in that PHP class from my custom JS?
checkout_cart_index.xml
<referenceBlock name="checkout.cart.totals">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="block-totals" xsi:type="array">
<item name="children" xsi:type="array">
<item name="grand-total" xsi:type="array">
<item name="component" xsi:type="string">My_Module/js/view/summary</item>
<item name="config" xsi:type="array">
<item name="title" xsi:type="string" translate="true">Order Total</item>
<item name="template" xsi:type="string">My_Module/cart/totals/grand-total</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
my-module/view/frontend/web/js/view/summary.js
define([
'Magento_Checkout/js/view/summary/abstract-total',
'Magento_Checkout/js/model/quote'
], function (Component, quote) {
'use strict';
return Component.extend({
defaults: {
template: 'My_Module/summary/grand-total'
},
initialize: function () {
this._super();
},
myCustomData: function () {
// get data returned in PHP
}
});
});
So what I need is to get the data returned as a php array using the method myCustomData
from my JS file.
I there any way to set the PHP class as an argument in the layout or something similar?
Best Answer
You can add your custom data to window.checkoutConfig object from your custom module
app/code/Anshu/Custom/etc/frontend/di.xml
app/code/Anshu/Custom/Model/CustomConfigProvider.php
app/code/Anshu/Custom/view/frontend/web/js/custom.js
Now you can call getCustomData in the html template.
app/code/Anshu/Custom/view/frontend/web/template/custom.html
This is sample code, you can try it and modify it according to your requirement.