Skip to content

Commit c1282c4

Browse files
committed
MC-19515: Cart price rule based on payment methods not aplied in checkout
1 parent f758758 commit c1282c4

File tree

9 files changed

+119
-145
lines changed

9 files changed

+119
-145
lines changed

app/code/Magento/Checkout/view/frontend/web/js/action/select-payment-method.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
* @api
88
*/
99
define([
10-
'Magento_Checkout/js/model/quote',
11-
'Magento_SalesRule/js/model/payment/discount-messages',
12-
'Magento_Checkout/js/action/set-payment-information',
13-
'Magento_Checkout/js/action/get-totals'
14-
], function (quote, messageContainer, setPaymentInformationAction, getTotalsAction) {
10+
'Magento_Checkout/js/model/quote'
11+
], function (quote) {
1512
'use strict';
1613

1714
return function (paymentMethod) {
@@ -21,15 +18,5 @@ define([
2118
};
2219
}
2320
quote.paymentMethod(paymentMethod);
24-
setPaymentInformationAction(
25-
messageContainer,
26-
{
27-
method: paymentMethod.method
28-
}
29-
);
30-
31-
if (quote.totals()['coupon_code']) {
32-
getTotalsAction([]);
33-
}
3421
};
3522
});

app/code/Magento/CheckoutAgreements/Model/Checkout/Plugin/GuestValidation.php

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;
1212

1313
/**
14-
* Class GuestValidation
14+
* Guest checkout agreements validation.
1515
*
1616
* Plugin that checks if checkout agreement enabled and validates all agreements.
1717
* Current plugin is duplicate from Magento\CheckoutAgreements\Model\Checkout\Plugin\Validation due to different
@@ -58,6 +58,8 @@ public function __construct(
5858
}
5959

6060
/**
61+
* Validates agreements before save payment information and order placing.
62+
*
6163
* @param \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject
6264
* @param string $cartId
6365
* @param string $email
@@ -80,28 +82,8 @@ public function beforeSavePaymentInformationAndPlaceOrder(
8082
}
8183

8284
/**
83-
* @param \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject
84-
* @param string $cartId
85-
* @param string $email
86-
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
87-
* @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
88-
* @throws \Magento\Framework\Exception\CouldNotSaveException
89-
* @return void
90-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
91-
*/
92-
public function beforeSavePaymentInformation(
93-
\Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject,
94-
$cartId,
95-
$email,
96-
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
97-
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
98-
) {
99-
if ($this->isAgreementEnabled()) {
100-
$this->validateAgreements($paymentMethod);
101-
}
102-
}
103-
104-
/**
85+
* Validates agreements.
86+
*
10587
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
10688
* @throws \Magento\Framework\Exception\CouldNotSaveException
10789
* @return void
@@ -123,7 +105,8 @@ private function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $pa
123105
}
124106

125107
/**
126-
* Verify if agreement validation needed
108+
* Verify if agreement validation needed.
109+
*
127110
* @return bool
128111
*/
129112
private function isAgreementEnabled()

app/code/Magento/CheckoutAgreements/Model/Checkout/Plugin/Validation.php

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;
1212

1313
/**
14-
* Class Validation
14+
* Checkout agreements validation.
1515
*/
1616
class Validation
1717
{
1818
/**
1919
* @var \Magento\Framework\App\Config\ScopeConfigInterface
2020
*/
21-
protected $scopeConfiguration;
21+
private $scopeConfiguration;
2222

2323
/**
2424
* @var \Magento\Checkout\Api\AgreementsValidatorInterface
2525
*/
26-
protected $agreementsValidator;
26+
private $agreementsValidator;
2727

2828
/**
2929
* @var \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface
@@ -54,6 +54,8 @@ public function __construct(
5454
}
5555

5656
/**
57+
* Validates agreements before save payment information and order placing.
58+
*
5759
* @param \Magento\Checkout\Api\PaymentInformationManagementInterface $subject
5860
* @param int $cartId
5961
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
@@ -74,31 +76,13 @@ public function beforeSavePaymentInformationAndPlaceOrder(
7476
}
7577

7678
/**
77-
* @param \Magento\Checkout\Api\PaymentInformationManagementInterface $subject
78-
* @param int $cartId
79-
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
80-
* @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
81-
* @throws \Magento\Framework\Exception\CouldNotSaveException
82-
* @return void
83-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
84-
*/
85-
public function beforeSavePaymentInformation(
86-
\Magento\Checkout\Api\PaymentInformationManagementInterface $subject,
87-
$cartId,
88-
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
89-
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
90-
) {
91-
if ($this->isAgreementEnabled()) {
92-
$this->validateAgreements($paymentMethod);
93-
}
94-
}
95-
96-
/**
79+
* Validates agreements.
80+
*
9781
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
9882
* @throws \Magento\Framework\Exception\CouldNotSaveException
9983
* @return void
10084
*/
101-
protected function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod)
85+
private function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod)
10286
{
10387
$agreements = $paymentMethod->getExtensionAttributes() === null
10488
? []
@@ -115,10 +99,11 @@ protected function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $
11599
}
116100

117101
/**
118-
* Verify if agreement validation needed
102+
* Verify if agreement validation needed.
103+
*
119104
* @return bool
120105
*/
121-
protected function isAgreementEnabled()
106+
private function isAgreementEnabled()
122107
{
123108
$isAgreementsEnabled = $this->scopeConfiguration->isSetFlag(
124109
AgreementsProvider::PATH_ENABLED,

app/code/Magento/CheckoutAgreements/Test/Unit/Model/Checkout/Plugin/GuestValidationTest.php

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\Store\Model\ScopeInterface;
1111

1212
/**
13-
* Class GuestValidationTest
1413
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1514
*/
1615
class GuestValidationTest extends \PHPUnit\Framework\TestCase
@@ -109,7 +108,7 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
109108
$this->paymentMock->expects(static::atLeastOnce())
110109
->method('getExtensionAttributes')
111110
->willReturn($this->extensionAttributesMock);
112-
$this->model->beforeSavePaymentInformation(
111+
$this->model->beforeSavePaymentInformationAndPlaceOrder(
113112
$this->subjectMock,
114113
$cartId,
115114
$email,
@@ -144,7 +143,7 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
144143
$this->paymentMock->expects(static::atLeastOnce())
145144
->method('getExtensionAttributes')
146145
->willReturn($this->extensionAttributesMock);
147-
$this->model->beforeSavePaymentInformation(
146+
$this->model->beforeSavePaymentInformationAndPlaceOrder(
148147
$this->subjectMock,
149148
$cartId,
150149
$email,
@@ -156,36 +155,4 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
156155
"The order wasn't placed. First, agree to the terms and conditions, then try placing your order again."
157156
);
158157
}
159-
160-
public function testBeforeSavePaymentInformation()
161-
{
162-
$cartId = 100;
163-
$email = 'email@example.com';
164-
$agreements = [1, 2, 3];
165-
$this->scopeConfigMock
166-
->expects($this->once())
167-
->method('isSetFlag')
168-
->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
169-
->willReturn(true);
170-
$searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
171-
$this->agreementsFilterMock->expects($this->once())
172-
->method('buildSearchCriteria')
173-
->willReturn($searchCriteriaMock);
174-
$this->checkoutAgreementsListMock->expects($this->once())
175-
->method('getList')
176-
->with($searchCriteriaMock)
177-
->willReturn([1]);
178-
$this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
179-
$this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(true);
180-
$this->paymentMock->expects(static::atLeastOnce())
181-
->method('getExtensionAttributes')
182-
->willReturn($this->extensionAttributesMock);
183-
$this->model->beforeSavePaymentInformation(
184-
$this->subjectMock,
185-
$cartId,
186-
$email,
187-
$this->paymentMock,
188-
$this->addressMock
189-
);
190-
}
191158
}

app/code/Magento/CheckoutAgreements/Test/Unit/Model/Checkout/Plugin/ValidationTest.php

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\Store\Model\ScopeInterface;
1111

1212
/**
13-
* Class ValidationTest
1413
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1514
*/
1615
class ValidationTest extends \PHPUnit\Framework\TestCase
@@ -108,7 +107,12 @@ public function testBeforeSavePaymentInformationAndPlaceOrder()
108107
$this->paymentMock->expects(static::atLeastOnce())
109108
->method('getExtensionAttributes')
110109
->willReturn($this->extensionAttributesMock);
111-
$this->model->beforeSavePaymentInformation($this->subjectMock, $cartId, $this->paymentMock, $this->addressMock);
110+
$this->model->beforeSavePaymentInformationAndPlaceOrder(
111+
$this->subjectMock,
112+
$cartId,
113+
$this->paymentMock,
114+
$this->addressMock
115+
);
112116
}
113117

114118
/**
@@ -136,35 +140,15 @@ public function testBeforeSavePaymentInformationAndPlaceOrderIfAgreementsNotVali
136140
$this->paymentMock->expects(static::atLeastOnce())
137141
->method('getExtensionAttributes')
138142
->willReturn($this->extensionAttributesMock);
139-
$this->model->beforeSavePaymentInformation($this->subjectMock, $cartId, $this->paymentMock, $this->addressMock);
143+
$this->model->beforeSavePaymentInformationAndPlaceOrder(
144+
$this->subjectMock,
145+
$cartId,
146+
$this->paymentMock,
147+
$this->addressMock
148+
);
140149

141150
$this->expectExceptionMessage(
142151
"The order wasn't placed. First, agree to the terms and conditions, then try placing your order again."
143152
);
144153
}
145-
146-
public function testBeforeSavePaymentInformation()
147-
{
148-
$cartId = 100;
149-
$agreements = [1, 2, 3];
150-
$this->scopeConfigMock
151-
->expects($this->once())
152-
->method('isSetFlag')
153-
->with(AgreementsProvider::PATH_ENABLED, ScopeInterface::SCOPE_STORE)
154-
->willReturn(true);
155-
$searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
156-
$this->agreementsFilterMock->expects($this->once())
157-
->method('buildSearchCriteria')
158-
->willReturn($searchCriteriaMock);
159-
$this->checkoutAgreementsListMock->expects($this->once())
160-
->method('getList')
161-
->with($searchCriteriaMock)
162-
->willReturn([1]);
163-
$this->extensionAttributesMock->expects($this->once())->method('getAgreementIds')->willReturn($agreements);
164-
$this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($agreements)->willReturn(true);
165-
$this->paymentMock->expects(static::atLeastOnce())
166-
->method('getExtensionAttributes')
167-
->willReturn($this->extensionAttributesMock);
168-
$this->model->beforeSavePaymentInformation($this->subjectMock, $cartId, $this->paymentMock, $this->addressMock);
169-
}
170154
}

app/code/Magento/SalesRule/Model/Quote/Discount.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ public function collect(
9696
$store = $this->storeManager->getStore($quote->getStoreId());
9797
$address = $shippingAssignment->getShipping()->getAddress();
9898

99-
// if ($quote->getPayment()->getMethod()) {
100-
// $address->setPaymentMethod($quote->getPayment()->getMethod());
101-
// }
99+
if ($quote->currentPaymentWasSet()) {
100+
$address->setPaymentMethod($quote->getPayment()->getMethod());
101+
}
102102

103103
$this->calculator->reset($address);
104104

app/code/Magento/SalesRule/view/frontend/web/js/action/select-payment-method-mixin.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
* See COPYING.txt for license details.
44
*/
55
define([
6+
'jquery',
67
'mage/utils/wrapper',
78
'Magento_Checkout/js/model/quote',
89
'Magento_SalesRule/js/model/payment/discount-messages',
910
'Magento_Checkout/js/action/set-payment-information',
10-
'Magento_Checkout/js/action/get-totals'
11-
], function (wrapper, quote, messageContainer, setPaymentInformationAction, getTotalsAction) {
11+
'Magento_Checkout/js/action/get-totals',
12+
'Magento_SalesRule/js/model/coupon'
13+
], function ($, wrapper, quote, messageContainer, setPaymentInformationAction, getTotalsAction, coupon) {
1214
'use strict';
1315

1416
return function (selectPaymentMethodAction) {
@@ -17,16 +19,32 @@ define([
1719

1820
originalSelectPaymentMethodAction(paymentMethod);
1921

20-
setPaymentInformationAction(
21-
messageContainer,
22-
{
23-
method: paymentMethod.method
22+
$.when(
23+
setPaymentInformationAction(
24+
messageContainer,
25+
{
26+
method: paymentMethod.method
27+
}
28+
)
29+
).done(
30+
function () {
31+
var deferred = $.Deferred(),
32+
33+
/**
34+
* Update coupon form.
35+
*/
36+
updateCouponCallback = function () {
37+
if (quote.totals() && !quote.totals()['coupon_code']) {
38+
coupon.setCouponCode('');
39+
coupon.setIsApplied(false);
40+
}
41+
};
42+
43+
getTotalsAction([], deferred);
44+
$.when(deferred).done(updateCouponCallback);
2445
}
2546
);
26-
27-
getTotalsAction([]);
2847
});
29-
3048
};
3149

3250
});

0 commit comments

Comments
 (0)