Skip to content

Commit 9a9b5ac

Browse files
committed
ACP2E-2850: emulate store as to load correct agreements based on quote store id
1 parent a2519ca commit 9a9b5ac

File tree

3 files changed

+151
-28
lines changed

3 files changed

+151
-28
lines changed

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

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,19 @@
66

77
namespace Magento\CheckoutAgreements\Model\Checkout\Plugin;
88

9+
use Magento\Checkout\Api\AgreementsValidatorInterface;
10+
use Magento\Checkout\Api\GuestPaymentInformationManagementInterface;
11+
use Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface;
912
use Magento\CheckoutAgreements\Model\AgreementsProvider;
13+
use Magento\CheckoutAgreements\Model\EmulateStore;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Framework\Exception\CouldNotSaveException;
16+
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\Quote\Api\CartRepositoryInterface;
18+
use Magento\Quote\Api\Data\AddressInterface;
19+
use Magento\Quote\Api\Data\PaymentInterface;
20+
use Magento\Quote\Model\MaskedQuoteIdToQuoteId;
21+
use Magento\Quote\Model\QuoteIdMask;
1022
use Magento\Store\Model\ScopeInterface;
1123
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;
1224

@@ -40,62 +52,96 @@ class GuestValidation
4052
private $activeStoreAgreementsFilter;
4153

4254
/**
43-
* @param \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator
44-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration
45-
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList
55+
* @var MaskedQuoteIdToQuoteId
56+
*/
57+
private MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId;
58+
59+
/**
60+
* @var CartRepositoryInterface
61+
*/
62+
private CartRepositoryInterface $quoteRepository;
63+
64+
/**
65+
* @var EmulateStore
66+
*/
67+
private EmulateStore $emulateStore;
68+
69+
/**
70+
* @param AgreementsValidatorInterface $agreementsValidator
71+
* @param ScopeConfigInterface $scopeConfiguration
72+
* @param CheckoutAgreementsListInterface $checkoutAgreementsList
4673
* @param ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
74+
* @param MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId
75+
* @param CartRepositoryInterface $quoteRepository
76+
* @param EmulateStore $emulateStore
4777
*/
4878
public function __construct(
4979
\Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
5080
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
5181
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList,
52-
\Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
82+
\Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter,
83+
MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId,
84+
CartRepositoryInterface $quoteRepository,
85+
EmulateStore $emulateStore
5386
) {
5487
$this->agreementsValidator = $agreementsValidator;
5588
$this->scopeConfiguration = $scopeConfiguration;
5689
$this->checkoutAgreementsList = $checkoutAgreementsList;
5790
$this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter;
91+
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
92+
$this->quoteRepository = $quoteRepository;
93+
$this->emulateStore = $emulateStore;
5894
}
5995

6096
/**
6197
* Validates agreements before save payment information and order placing.
6298
*
63-
* @param \Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject
99+
* @param GuestPaymentInformationManagementInterface $subject
64100
* @param string $cartId
65101
* @param string $email
66-
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
67-
* @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
68-
* @throws \Magento\Framework\Exception\CouldNotSaveException
102+
* @param PaymentInterface $paymentMethod
103+
* @param AddressInterface|null $billingAddress
69104
* @return void
70105
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
106+
* @throws CouldNotSaveException|NoSuchEntityException
71107
*/
72108
public function beforeSavePaymentInformationAndPlaceOrder(
73-
\Magento\Checkout\Api\GuestPaymentInformationManagementInterface $subject,
109+
GuestPaymentInformationManagementInterface $subject,
74110
$cartId,
75111
$email,
76-
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
77-
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
112+
PaymentInterface $paymentMethod,
113+
AddressInterface $billingAddress = null
78114
) {
79115
if ($this->isAgreementEnabled()) {
80-
$this->validateAgreements($paymentMethod);
116+
$quoteId = $this->maskedQuoteIdToQuoteId->execute($cartId);
117+
$quote = $this->quoteRepository->get($quoteId);
118+
$storeId = $quote->getStoreId();
119+
$this->validateAgreements($paymentMethod, $storeId);
81120
}
82121
}
83122

84123
/**
85124
* Validates agreements.
86125
*
87-
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
88-
* @throws \Magento\Framework\Exception\CouldNotSaveException
126+
* @param PaymentInterface $paymentMethod
127+
* @param int $storeId
89128
* @return void
129+
* @throws CouldNotSaveException
90130
*/
91-
private function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod)
131+
private function validateAgreements(PaymentInterface $paymentMethod, int $storeId)
92132
{
93133
$agreements = $paymentMethod->getExtensionAttributes() === null
94134
? []
95135
: $paymentMethod->getExtensionAttributes()->getAgreementIds();
96136

97-
if (!$this->agreementsValidator->isValid($agreements)) {
98-
throw new \Magento\Framework\Exception\CouldNotSaveException(
137+
$isValid = $this->emulateStore->execute(
138+
$storeId,
139+
$this->agreementsValidator->isValid(...),
140+
[$agreements]
141+
);
142+
143+
if (!$isValid) {
144+
throw new CouldNotSaveException(
99145
__(
100146
"The order wasn't placed. "
101147
. "First, agree to the terms and conditions, then try placing your order again."

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

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66

77
namespace Magento\CheckoutAgreements\Model\Checkout\Plugin;
88

9+
use Magento\Checkout\Api\AgreementsValidatorInterface;
10+
use Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface;
911
use Magento\CheckoutAgreements\Model\AgreementsProvider;
1012
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;
13+
use Magento\CheckoutAgreements\Model\EmulateStore;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Framework\Exception\CouldNotSaveException;
1116
use Magento\Quote\Api\CartRepositoryInterface;
17+
use Magento\Quote\Api\Data\PaymentInterface;
1218
use Magento\Store\Model\ScopeInterface;
1319

1420
/**
@@ -37,31 +43,37 @@ class Validation
3743
private $activeStoreAgreementsFilter;
3844

3945
/**
40-
* Quote repository.
41-
*
4246
* @var \Magento\Quote\Api\CartRepositoryInterface
4347
*/
4448
private $quoteRepository;
4549

4650
/**
47-
* @param \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator
48-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration
49-
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList
51+
* @var EmulateStore
52+
*/
53+
private EmulateStore $emulateStore;
54+
55+
/**
56+
* @param AgreementsValidatorInterface $agreementsValidator
57+
* @param ScopeConfigInterface $scopeConfiguration
58+
* @param CheckoutAgreementsListInterface $checkoutAgreementsList
5059
* @param ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
5160
* @param CartRepositoryInterface $quoteRepository
61+
* @param EmulateStore $emulateStore
5262
*/
5363
public function __construct(
5464
\Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
5565
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
5666
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList,
5767
\Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter,
58-
CartRepositoryInterface $quoteRepository
68+
CartRepositoryInterface $quoteRepository,
69+
EmulateStore $emulateStore
5970
) {
6071
$this->agreementsValidator = $agreementsValidator;
6172
$this->scopeConfiguration = $scopeConfiguration;
6273
$this->checkoutAgreementsList = $checkoutAgreementsList;
6374
$this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter;
6475
$this->quoteRepository = $quoteRepository;
76+
$this->emulateStore = $emulateStore;
6577
}
6678

6779
/**
@@ -82,24 +94,33 @@ public function beforeSavePaymentInformationAndPlaceOrder(
8294
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
8395
) {
8496
if ($this->isAgreementEnabled()) {
85-
$this->validateAgreements($paymentMethod);
97+
$quote = $this->quoteRepository->get($cartId);
98+
$storeId = $quote->getStoreId();
99+
$this->validateAgreements($paymentMethod, $storeId);
86100
}
87101
}
88102

89103
/**
90104
* Validate agreements base on the payment method
91105
*
92-
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
93-
* @throws \Magento\Framework\Exception\CouldNotSaveException
106+
* @param PaymentInterface $paymentMethod
107+
* @param int $storeId
94108
* @return void
109+
* @throws CouldNotSaveException
95110
*/
96-
protected function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod)
111+
private function validateAgreements(\Magento\Quote\Api\Data\PaymentInterface $paymentMethod, int $storeId)
97112
{
98113
$agreements = $paymentMethod->getExtensionAttributes() === null
99114
? []
100115
: $paymentMethod->getExtensionAttributes()->getAgreementIds();
101116

102-
if (!$this->agreementsValidator->isValid($agreements)) {
117+
$isValid = $this->emulateStore->execute(
118+
$storeId,
119+
$this->agreementsValidator->isValid(...),
120+
[$agreements]
121+
);
122+
123+
if (!$isValid) {
103124
throw new \Magento\Framework\Exception\CouldNotSaveException(
104125
__(
105126
"The order wasn't placed. "
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\CheckoutAgreements\Model;
20+
21+
use Magento\Framework\Exception\NoSuchEntityException;
22+
use Magento\Store\Model\StoreManager;
23+
24+
class EmulateStore
25+
{
26+
27+
/**
28+
* @param StoreManager $storeManager
29+
*/
30+
public function __construct(
31+
private StoreManager $storeManager
32+
) {
33+
}
34+
35+
/**
36+
* Execute callable with emulated store
37+
*
38+
* @param int $storeId
39+
* @param callable $callable
40+
* @param array $args
41+
* @return mixed
42+
* @throws NoSuchEntityException
43+
*/
44+
public function execute(int $storeId, callable $callable, array $args = []): mixed
45+
{
46+
$currentStoreId = (int)$this->storeManager->getStore()->getId();
47+
if ($currentStoreId !== $storeId) {
48+
$this->storeManager->setCurrentStore($storeId);
49+
}
50+
$result = $callable(...$args);
51+
if ($currentStoreId !== $storeId) {
52+
$this->storeManager->setCurrentStore($currentStoreId);
53+
}
54+
return $result;
55+
}
56+
}

0 commit comments

Comments
 (0)