Magento 2: Pass variable from phtml to js
In your .phtml please add following code.
<script type="text/x-magento-init">
{
"*": {
"Magepow_Ajaxcart/js/view/minicartaddons": {
"maxpriceShipping": <?= /* @escapeNotVerified */ $maxpriceShipping ?>
}
}
}
And in your JS file please add below code to get maxpriceShipping
define([
'ko',
'uiComponent',
'Magento_Customer/js/customer-data',
], function (ko, Component, customerData) {
'use strict';
var subtotalAmount;
var percentage;
return Component.extend({
displaySubtotal: ko.observable(true),
maxprice:maxPrice.toFixed(2),
/**
* @override
*/
initialize: function () {
this._super();
this.cart = customerData.get('cart');
/* Add this code to get maxpriceShipping value*/
/* You can use this variable to call this function in your JS*/
this.maxPrice = ko.observable(this.maxpriceShipping);
/* Code End*/
},
getTotalCartItems: function () {
return customerData.get('cart')().summary_count;
},
getpercentage: function () {
subtotalAmount = customerData.get('cart')().subtotalAmount;
if (subtotalAmount > this.maxPrice) {
subtotalAmount = this.maxPrice;
}
percentage = ((subtotalAmount * 100) / this.maxPrice);
return percentage;
}
});
});
Please check and let me know in case of any issue.