Skip to content

Commit 7a95761

Browse files
committed
Merge remote-tracking branch 'origin/AC-12170' into spartans_pr_04072024
2 parents 0c53bbf + 7f5ac99 commit 7a95761

File tree

2 files changed

+76
-68
lines changed

2 files changed

+76
-68
lines changed

app/code/Magento/SalesRule/view/frontend/web/js/view/payment/captcha.js

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,66 @@
44
*/
55

66
define([
7-
'Magento_Captcha/js/view/checkout/defaultCaptcha',
8-
'Magento_Captcha/js/model/captchaList',
9-
'Magento_SalesRule/js/action/set-coupon-code',
10-
'Magento_SalesRule/js/action/cancel-coupon',
11-
'Magento_Checkout/js/model/quote',
12-
'ko'
13-
],
14-
function (defaultCaptcha, captchaList, setCouponCodeAction, cancelCouponAction, quote, ko) {
15-
'use strict';
7+
'Magento_Captcha/js/view/checkout/defaultCaptcha',
8+
'Magento_Captcha/js/model/captchaList',
9+
'Magento_SalesRule/js/action/set-coupon-code',
10+
'Magento_SalesRule/js/action/cancel-coupon',
11+
'Magento_Checkout/js/model/quote',
12+
'ko'
13+
], function (defaultCaptcha, captchaList, setCouponCodeAction, cancelCouponAction, quote, ko) {
14+
'use strict';
1615

17-
var totals = quote.getTotals(),
18-
couponCode = ko.observable(null),
19-
isApplied;
16+
return defaultCaptcha.extend({
17+
/** @inheritdoc */
18+
initialize: function () {
19+
var self = this,
20+
currentCaptcha,
21+
totals = quote.getTotals(),
22+
couponCode = ko.observable(null),
23+
couponCodeValue,
24+
isApplied;
2025

21-
if (totals()) {
22-
couponCode(totals()['coupon_code']);
23-
}
24-
//Captcha can only be required for adding a coupon so we need to know if one was added already.
25-
isApplied = ko.observable(couponCode() != null);
26+
if (totals()) {
27+
couponCode(totals()['coupon_code']);
28+
}
2629

27-
return defaultCaptcha.extend({
28-
/** @inheritdoc */
29-
initialize: function () {
30-
var self = this,
31-
currentCaptcha;
30+
// Captcha can only be required for adding a coupon so we need to know if one was added already.
31+
couponCodeValue = couponCode();
32+
isApplied = ko.observable(typeof couponCodeValue === 'string' && couponCodeValue.length > 0);
3233

33-
this._super();
34-
//Getting coupon captcha model.
35-
currentCaptcha = captchaList.getCaptchaByFormId(this.formId);
34+
this._super();
35+
//Getting coupon captcha model.
36+
currentCaptcha = captchaList.getCaptchaByFormId(this.formId);
3637

37-
if (currentCaptcha != null) {
38-
if (!isApplied()) {
39-
//Show captcha if we don't have a coupon applied.
40-
currentCaptcha.setIsVisible(true);
41-
}
42-
this.setCurrentCaptcha(currentCaptcha);
43-
//Add captcha code to coupon-apply request.
44-
setCouponCodeAction.registerDataModifier(function (headers) {
45-
if (self.isRequired()) {
46-
headers['X-Captcha'] = self.captchaValue()();
47-
}
48-
});
49-
//Refresh captcha after failed request.
50-
setCouponCodeAction.registerFailCallback(function () {
51-
if (self.isRequired()) {
52-
self.refresh();
53-
}
54-
});
55-
//Hide captcha when a coupon has been applied.
56-
setCouponCodeAction.registerSuccessCallback(function () {
57-
self.setIsVisible(false);
58-
});
59-
//Show captcha again if it was canceled.
60-
cancelCouponAction.registerSuccessCallback(function () {
61-
if (self.isRequired()) {
62-
self.setIsVisible(true);
63-
}
64-
});
38+
if (currentCaptcha != null) {
39+
if (!isApplied()) {
40+
//Show captcha if we don't have a coupon applied.
41+
currentCaptcha.setIsVisible(true);
6542
}
43+
this.setCurrentCaptcha(currentCaptcha);
44+
//Add captcha code to coupon-apply request.
45+
setCouponCodeAction.registerDataModifier(function (headers) {
46+
if (self.isRequired()) {
47+
headers['X-Captcha'] = self.captchaValue()();
48+
}
49+
});
50+
//Refresh captcha after failed request.
51+
setCouponCodeAction.registerFailCallback(function () {
52+
if (self.isRequired()) {
53+
self.refresh();
54+
}
55+
});
56+
//Hide captcha when a coupon has been applied.
57+
setCouponCodeAction.registerSuccessCallback(function () {
58+
self.setIsVisible(false);
59+
});
60+
//Show captcha again if it was canceled.
61+
cancelCouponAction.registerSuccessCallback(function () {
62+
if (self.isRequired()) {
63+
self.setIsVisible(true);
64+
}
65+
});
6666
}
67-
});
67+
}
6868
});
69+
});

app/code/Magento/SalesRule/view/frontend/web/js/view/payment/discount.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,39 @@ define([
1414
], function ($, ko, Component, quote, setCouponCodeAction, cancelCouponAction, coupon) {
1515
'use strict';
1616

17-
var totals = quote.getTotals(),
18-
couponCode = coupon.getCouponCode(),
19-
isApplied = coupon.getIsApplied();
20-
21-
if (totals()) {
22-
couponCode(totals()['coupon_code']);
23-
}
24-
isApplied(couponCode() != null);
25-
2617
return Component.extend({
2718
defaults: {
2819
template: 'Magento_SalesRule/payment/discount'
2920
},
30-
couponCode: couponCode,
21+
couponCode: coupon.getCouponCode(),
3122

3223
/**
3324
* Applied flag
3425
*/
35-
isApplied: isApplied,
26+
isApplied: coupon.getIsApplied(),
27+
28+
initialize: function () {
29+
var totals = quote.getTotals(),
30+
couponCode = this.couponCode,
31+
isApplied = this.isApplied,
32+
couponCodeValue;
33+
34+
if (totals()) {
35+
couponCode(totals()['coupon_code']);
36+
}
37+
38+
couponCodeValue = couponCode();
39+
isApplied(typeof couponCodeValue === 'string' && couponCodeValue.length > 0);
40+
41+
this._super();
42+
},
3643

3744
/**
3845
* Coupon code application procedure
3946
*/
4047
apply: function () {
4148
if (this.validate()) {
42-
setCouponCodeAction(couponCode(), isApplied);
49+
setCouponCodeAction(this.couponCode(), this.isApplied);
4350
}
4451
},
4552

@@ -48,8 +55,8 @@ define([
4855
*/
4956
cancel: function () {
5057
if (this.validate()) {
51-
couponCode('');
52-
cancelCouponAction(isApplied);
58+
this.couponCode('');
59+
cancelCouponAction(this.isApplied);
5360
}
5461
},
5562

0 commit comments

Comments
 (0)