I added a step in Magento 2 checkout process. by extending a template using .js file
return Component.extend({
defaults: {
template: 'vendor_module/mytemplate.phtml'
},
And it is working fine. but i need to add some dynamic content to the page, fetching data from a custom table in db and display them in the template.
How can I achieve that. Adding My code bellow
Js File
define(
[
'ko',
'uiComponent',
'underscore',
'Magento_Checkout/js/model/step-navigator',
'Magento_Customer/js/model/customer'
],
function (
ko,
Component,
_,
stepNavigator,
customer
) {
'use strict';
return Component.extend({
defaults: {
template: 'vendor_module/template_file.phtml'
},
isVisible: ko.observable(true),
isLogedIn: customer.isLoggedIn(),
stepCode: 'stepCode',
stepTitle: 'stepTitle',
initialize: function () {
this._super();
stepNavigator.registerStep(
this.stepCode,
null,
this.stepTitle,
this.isVisible,
_.bind(this.navigate, this),
15
);
return this;
},
navigate: function () {
},
navigateToNextStep: function () {
stepNavigator.next();
},
getCustomData: function () {
var customData = window.checkoutConfig.customData;
return customData;
},
});
}
);
PHP File
<?php
namespace vendor\module\Model;
use Magento\Checkout\Model\ConfigProviderInterface;
class CustomConfigProvider implements ConfigProviderInterface
{
public function getConfig()
{
$config = [];
$config['customData'] = 'Your text.';
return $config;
}
}
template_file.phtml
<!--Use 'stepCode' as id attribute-->
<li data-bind="fadeVisible: isVisible, attr: { id: stepCode }">
<div class="step-title" data-bind="i18n: stepTitle" data-role="title"></div>
<div id="checkout-step-title"
class="step-content"
data-role="content">
<div class="component-wrapper">
<div data-bind="text: getCustomData()"></div>
</div>
<!-- other html contents -->
<!-- other html contents -->
<!-- other html contents -->
</div>
</li>