This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,4 @@
define(['jquery', 'mage/url', 'mage/cookies'], function ($, urlBuilder) {
'use strict';
return { apply: function (points) { return $.ajax({url: urlBuilder.build('reward/cart/apply'), type: 'POST', dataType: 'json', data: {form_key: $.mage.cookies.get('form_key'), points: points}}); } };
});
@@ -0,0 +1,12 @@
define(['jquery', 'mage/translate', 'Magento_Customer/js/customer-data'], function ($, $t, customerData) {
'use strict';
return function (config, element) {
$(element).on('submit', function (event) {
event.preventDefault(); var $form = $(this), $message = $form.find('[data-role=esun-reward-message]');
$.ajax({url: $form.data('url'), type: 'POST', dataType: 'json', data: $form.serialize(), showLoader: true})
.done(function (response) { $message.text(response.message || ''); customerData.reload(['cart'], true).always(function () { window.location.reload(); }); })
.fail(function (xhr) { $message.text((xhr.responseJSON && xhr.responseJSON.message) || $t('Unable to apply points.')); });
return false;
});
};
});
@@ -0,0 +1,30 @@
define([
'jquery',
'Magento_Checkout/js/action/get-totals',
'Magento_Customer/js/customer-data'
], function ($, getTotalsAction, customerData) {
'use strict';
return function (Discount) {
return Discount.extend({
initialize: function () {
this._super();
$(document)
.off('esun:customerCouponApplied.esunCheckoutDiscount')
.on('esun:customerCouponApplied.esunCheckoutDiscount',
this.onCustomerCouponApplied.bind(this));
return this;
},
onCustomerCouponApplied: function (event, response) {
var code = response && response.coupon_code || '';
this.couponCode(code);
this.isApplied(Boolean(code));
customerData.reload(['cart'], true);
getTotalsAction([]);
}
});
};
});
@@ -0,0 +1,77 @@
define([
'uiComponent',
'ko',
'jquery',
'mage/translate',
'mage/url',
'Magento_Customer/js/customer-data',
'Esun_Reward/js/model/reward-service',
'Magento_Checkout/js/action/get-totals'
], function (Component, ko, $, $t, urlBuilder, customerData, rewardService, getTotalsAction) {
'use strict';
return Component.extend({
defaults: {template: 'Esun_Reward/payment/reward'},
initialize: function () {
var self = this,
checkoutReward = window.checkoutConfig.esunReward || {},
cartReward;
this._super();
this.cart = customerData.get('cart');
this.customer = customerData.get('customer');
cartReward = this.cart().reward || {};
this.rewardData = ko.observable($.extend({
logged_in: false,
balance: 0,
applied_points: 0,
maximum: 1500,
rate: 100
}, cartReward, checkoutReward));
this.points = ko.observable(Number(this.rewardData().applied_points || 0));
this.message = ko.observable('');
this.loading = ko.observable(false);
this.ready = ko.observable(false);
this.cart.subscribe(function (cart) {
if (cart.reward) {
self.rewardData(cart.reward);
}
});
this.refreshRewardData();
return this;
},
reward: function () { return this.rewardData(); },
isLoggedIn: function () {
var customer = this.customer();
return Boolean(this.reward().logged_in || customer.fullname || customer.firstname);
},
refreshRewardData: function () {
var self = this;
$.ajax({
url: urlBuilder.build('customer/section/load/'),
type: 'get',
dataType: 'json',
cache: false,
data: {
sections: 'cart,customer',
force_new_section_timestamp: true
}
}).done(function (sections) {
if (sections.cart) {
customerData.set('cart', sections.cart);
if (sections.cart.reward) {
self.rewardData(sections.cart.reward);
}
self.points(Number((sections.cart.reward || {}).applied_points || 0));
}
if (sections.customer) {
customerData.set('customer', sections.customer);
}
}).always(function () {
self.ready(true);
});
},
apply: function () { var self = this; if (this.loading()) { return false; } this.loading(true); rewardService.apply(Math.max(0, parseInt(this.points(), 10) || 0)).done(function (response) { self.message(response.message || ''); customerData.reload(['cart'], true); getTotalsAction([]); }).fail(function (xhr) { self.message((xhr.responseJSON && xhr.responseJSON.message) || $t('Unable to apply points.')); }).always(function () { self.loading(false); }); return false; }
});
});
@@ -0,0 +1,26 @@
define(['Magento_Checkout/js/view/summary/abstract-total', 'Magento_Checkout/js/model/quote'], function (Component, quote) {
'use strict';
return Component.extend({
defaults: {template: 'Esun_Reward/summary/reward-discount'},
getSegment: function () {
var totals = quote.getTotals()();
return totals && totals.total_segments && totals.total_segments.filter(function (item) {
return item.code === 'reward_discount';
})[0];
},
isDisplayed: function () {
return this.getPureValue() !== 0;
},
getPureValue: function () {
var segment = this.getSegment();
return segment ? Number(segment.value) : 0;
},
getTitle: function () {
var segment = this.getSegment();
return segment && segment.title ? segment.title : this.title;
},
getValue: function () {
return this.getFormattedPrice(this.getPureValue());
}
});
});