Skip to content

Commit b8a89a4

Browse files
committed
ACP2E-3255: [GRAPHQL] model value should be specified when getting customerCart
- Fixed the issue.
1 parent 0dd73ec commit b8a89a4

File tree

3 files changed

+95
-30
lines changed

3 files changed

+95
-30
lines changed

app/code/Magento/Quote/Model/QuoteManagement.php

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
use Magento\Sales\Api\Data\OrderInterfaceFactory as OrderFactory;
4444
use Magento\Sales\Api\OrderManagementInterface as OrderManagement;
4545
use Magento\Store\Model\StoreManagerInterface;
46-
use Magento\Customer\Model\AddressRegistry;
4746

4847
/**
4948
* Class for managing quote
@@ -184,11 +183,6 @@ class QuoteManagement implements CartManagementInterface, ResetAfterRequestInter
184183
*/
185184
private $cartMutex;
186185

187-
/**
188-
* @var AddressRegistry
189-
*/
190-
private $addressRegistry;
191-
192186
/**
193187
* @param EventManager $eventManager
194188
* @param SubmitQuoteValidator $submitQuoteValidator
@@ -216,7 +210,6 @@ class QuoteManagement implements CartManagementInterface, ResetAfterRequestInter
216210
* @param RemoteAddress|null $remoteAddress
217211
* @param LockManagerInterface $lockManager
218212
* @param CartMutexInterface|null $cartMutex
219-
* @param AddressRegistry|null $addressRegistry
220213
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
221214
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
222215
*/
@@ -246,8 +239,7 @@ public function __construct(
246239
RequestInterface $request = null,
247240
RemoteAddress $remoteAddress = null,
248241
LockManagerInterface $lockManager = null,
249-
?CartMutexInterface $cartMutex = null,
250-
?AddressRegistry $addressRegistry = null
242+
?CartMutexInterface $cartMutex = null
251243
) {
252244
$this->eventManager = $eventManager;
253245
$this->submitQuoteValidator = $submitQuoteValidator;
@@ -279,8 +271,6 @@ public function __construct(
279271
->get(RemoteAddress::class);
280272
$this->cartMutex = $cartMutex
281273
?? ObjectManager::getInstance()->get(CartMutexInterface::class);
282-
$this->addressRegistry = $addressRegistry
283-
?? ObjectManager::getInstance()->get(AddressRegistry::class);
284274
}
285275

286276
/**
@@ -706,9 +696,7 @@ protected function _prepareCustomerQuote($quote)
706696
}
707697
//save here new customer address
708698
$shippingAddress->setCustomerId($quote->getCustomerId());
709-
if ($this->validateAddress((int) $shippingAddress->getId()) === true) {
710-
$this->addressRepository->save($shippingAddress);
711-
}
699+
$this->addressRepository->save($shippingAddress);
712700
$quote->addCustomerAddress($shippingAddress);
713701
$shipping->setCustomerAddressData($shippingAddress);
714702
$this->addressesToSync[] = $shippingAddress->getId();
@@ -740,9 +728,7 @@ protected function _prepareCustomerQuote($quote)
740728
$billingAddress->setIsDefaultBilling(true);
741729
}
742730
$billingAddress->setCustomerId($quote->getCustomerId());
743-
if ($this->validateAddress((int) $billingAddress->getId()) === true) {
744-
$this->addressRepository->save($billingAddress);
745-
}
731+
$this->addressRepository->save($billingAddress);
746732
$quote->addCustomerAddress($billingAddress);
747733
$billing->setCustomerAddressData($billingAddress);
748734
$this->addressesToSync[] = $billingAddress->getId();
@@ -805,17 +791,4 @@ public function _resetState(): void
805791
{
806792
$this->addressesToSync = [];
807793
}
808-
809-
/**
810-
* Validate address
811-
*
812-
* @param int|null $addressId
813-
* @return array|bool
814-
* @throws NoSuchEntityException
815-
*/
816-
private function validateAddress(?int $addressId): bool|array
817-
{
818-
$addressModel = $this->addressRegistry->retrieve($addressId);
819-
return $addressModel->validate();
820-
}
821794
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Plugin\Model;
9+
10+
use Magento\Framework\Exception\CouldNotSaveException;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Quote\Model\Quote;
14+
use Magento\Quote\Model\QuoteFactory;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\Quote\Api\CartRepositoryInterface;
17+
use Magento\Customer\Api\CustomerRepositoryInterface;
18+
use Closure;
19+
use Exception;
20+
use Magento\Quote\Model\QuoteManagement;
21+
22+
class CreateEmptyCartWithoutCountryValidation
23+
{
24+
/**
25+
* @param StoreManagerInterface $storeManager
26+
* @param CartRepositoryInterface $quoteRepository
27+
* @param CustomerRepositoryInterface $customerRepository
28+
* @param QuoteFactory $quoteFactory
29+
*/
30+
public function __construct(
31+
private readonly StoreManagerInterface $storeManager,
32+
private readonly CartRepositoryInterface $quoteRepository,
33+
private readonly CustomerRepositoryInterface $customerRepository,
34+
private readonly QuoteFactory $quoteFactory
35+
) {}
36+
37+
/**
38+
* Create empty cart for customer without country validation
39+
*
40+
* @param QuoteManagement $subject
41+
* @param Closure $proceed
42+
* @param int $customerId
43+
* @return bool|int
44+
* @throws CouldNotSaveException
45+
* @throws NoSuchEntityException
46+
* @throws LocalizedException
47+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
48+
*/
49+
public function aroundCreateEmptyCartForCustomer(
50+
QuoteManagement $subject,
51+
Closure $proceed,
52+
int $customerId
53+
): bool|int
54+
{
55+
$storeId = (int) $this->storeManager->getStore()->getStoreId();
56+
$quote = $this->createCustomerCart($customerId,$storeId);
57+
58+
try {
59+
$this->quoteRepository->save($quote);
60+
} catch (Exception $e) {
61+
throw new CouldNotSaveException(__("The quote can't be created."));
62+
}
63+
return (int)$quote->getId();
64+
}
65+
66+
/**
67+
* Creates a cart for the currently logged-in customer.
68+
*
69+
* @param int $customerId
70+
* @param int $storeId
71+
* @return Quote Cart object.
72+
* @throws NoSuchEntityException
73+
* @throws LocalizedException
74+
*/
75+
private function createCustomerCart(int $customerId, int $storeId): Quote
76+
{
77+
try {
78+
$quote = $this->quoteRepository->getActiveForCustomer($customerId);
79+
} catch (NoSuchEntityException $e) {
80+
$customer = $this->customerRepository->getById($customerId);
81+
$quote = $this->quoteFactory->create();
82+
$quote->setStoreId($storeId);
83+
$quote->setCustomer($customer);
84+
$quote->setCustomerIsGuest(0);
85+
}
86+
return $quote;
87+
}
88+
}

app/code/Magento/QuoteGraphQl/etc/graphql/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,8 @@
7979
<plugin name="merge_guest_orders_with_customer_after_place"
8080
type="Magento\QuoteGraphQl\Plugin\Model\MergeGuestOrder" />
8181
</type>
82+
<type name="Magento\Quote\Model\QuoteManagement">
83+
<plugin name="create_empty_cart_without_country_validation"
84+
type="Magento\QuoteGraphQl\Plugin\Model\CreateEmptyCartWithoutCountryValidation" />
85+
</type>
8286
</config>

0 commit comments

Comments
 (0)