Skip to content

Commit c18dd5a

Browse files
committed
ACP2E-3255: [GRAPHQL] model value should be specified when getting customerCart
- Fixed the CR comments.
1 parent 28edd56 commit c18dd5a

File tree

4 files changed

+117
-29
lines changed

4 files changed

+117
-29
lines changed

app/code/Magento/Quote/Model/Cart/CustomerCartResolver.php

+2-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Magento\Quote\Model\QuoteIdMaskFactory;
1515
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
1616
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResourceModel;
17-
use Magento\Quote\Model\CreateEmptyCartForCustomerWithoutCountryValidation;
1817

1918
/**
2019
* Get customer cart or create empty cart. Ensure mask_id is created
@@ -41,30 +40,22 @@ class CustomerCartResolver
4140
*/
4241
private $quoteIdToMaskedQuoteId;
4342

44-
/**
45-
* @var CreateEmptyCartForCustomerWithoutCountryValidation
46-
*/
47-
private $createEmptyCartForCustomerWithoutCountryValidation;
48-
4943
/**
5044
* @param CartManagementInterface $cartManagement
5145
* @param QuoteIdMaskFactory $quoteIdMaskFactory
5246
* @param QuoteIdMaskResourceModel $quoteIdMaskResourceModel
5347
* @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
54-
* @param CreateEmptyCartForCustomerWithoutCountryValidation $createEmptyCartForCustomerWithoutCountryValidation
5548
*/
5649
public function __construct(
5750
CartManagementInterface $cartManagement,
5851
QuoteIdMaskFactory $quoteIdMaskFactory,
5952
QuoteIdMaskResourceModel $quoteIdMaskResourceModel,
60-
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId,
61-
CreateEmptyCartForCustomerWithoutCountryValidation $createEmptyCartForCustomerWithoutCountryValidation
53+
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
6254
) {
6355
$this->cartManagement = $cartManagement;
6456
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
6557
$this->quoteIdMaskResourceModel = $quoteIdMaskResourceModel;
6658
$this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId;
67-
$this->createEmptyCartForCustomerWithoutCountryValidation = $createEmptyCartForCustomerWithoutCountryValidation;
6859
}
6960

7061
/**
@@ -82,8 +73,7 @@ public function resolve(int $customerId, string $predefinedMaskedQuoteId = null)
8273
/** @var Quote $cart */
8374
$cart = $this->cartManagement->getCartForCustomer($customerId);
8475
} catch (NoSuchEntityException $e) {
85-
$this->createEmptyCartForCustomerWithoutCountryValidation
86-
->createEmptyCartForCustomerWithoutCountryValidation($customerId);
76+
$this->cartManagement->createEmptyCartForCustomer($customerId);
8777
$cart = $this->cartManagement->getCartForCustomer($customerId);
8878
}
8979
try {

app/code/Magento/Quote/Model/CreateEmptyCartForCustomerWithoutCountryValidation.php app/code/Magento/Quote/Model/CreateEmptyCartForCustomer.php

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,51 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\Quote\Model;
99

1010
use Exception;
1111
use Magento\Customer\Api\CustomerRepositoryInterface;
12-
use Magento\Framework\App\Config\ScopeConfigInterface;
1312
use Magento\Framework\Exception\LocalizedException;
1413
use Magento\Framework\Exception\NoSuchEntityException;
1514
use Magento\Quote\Api\CartRepositoryInterface;
1615
use Magento\Store\Model\StoreManagerInterface;
1716
use Magento\Framework\Exception\CouldNotSaveException;
1817

1918
/**
20-
* Class for creating empty cart for customer without country validation
19+
* Class to create an empty cart and quote for a specified customer.
2120
*/
22-
class CreateEmptyCartForCustomerWithoutCountryValidation
21+
readonly class CreateEmptyCartForCustomer
2322
{
2423
/**
2524
* @param StoreManagerInterface $storeManager
2625
* @param CartRepositoryInterface $quoteRepository
2726
* @param CustomerRepositoryInterface $customerRepository
2827
* @param QuoteFactory $quoteFactory
29-
* @param ScopeConfigInterface $scopeConfig
3028
*/
3129
public function __construct(
32-
private readonly StoreManagerInterface $storeManager,
33-
private readonly CartRepositoryInterface $quoteRepository,
34-
private readonly CustomerRepositoryInterface $customerRepository,
35-
private readonly QuoteFactory $quoteFactory,
36-
private readonly ScopeConfigInterface $scopeConfig
30+
private StoreManagerInterface $storeManager,
31+
private CartRepositoryInterface $quoteRepository,
32+
private CustomerRepositoryInterface $customerRepository,
33+
private QuoteFactory $quoteFactory,
3734
) {
3835
}
3936

4037
/**
41-
* Create empty cart for customer without country validation
38+
* Creates an empty cart and quote for a specified customer if customer does not have a cart yet.
4239
*
4340
* @param int $customerId
44-
* @return bool|int
41+
* @return int
4542
* @throws LocalizedException
4643
* @throws NoSuchEntityException
4744
*/
48-
public function createEmptyCartForCustomerWithoutCountryValidation(int $customerId): bool|int
45+
public function execute(int $customerId): int
4946
{
5047
$storeId = (int) $this->storeManager->getStore()->getStoreId();
51-
$quote = $this->createCustomerCart($customerId, $storeId);
48+
$quote = $this->getCustomerActiveQuote($customerId, $storeId);
5249

5350
try {
5451
$this->quoteRepository->save($quote);
@@ -59,15 +56,15 @@ public function createEmptyCartForCustomerWithoutCountryValidation(int $customer
5956
}
6057

6158
/**
62-
* Creates a cart for the currently logged-in customer.
59+
* Get an active quote for the currently logged-in customer.
6360
*
6461
* @param int $customerId
6562
* @param int $storeId
6663
* @return Quote Cart object.
6764
* @throws NoSuchEntityException
6865
* @throws LocalizedException
6966
*/
70-
private function createCustomerCart(int $customerId, int $storeId): Quote
67+
private function getCustomerActiveQuote(int $customerId, int $storeId): Quote
7168
{
7269
try {
7370
$activeQuote = $this->quoteRepository->getActiveForCustomer($customerId);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
9+
namespace Magento\QuoteGraphQl\Plugin\Model\Cart;
10+
11+
use Magento\Framework\Exception\AlreadyExistsException;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Quote\Model\Cart\CustomerCartResolver;
15+
use Magento\Quote\Model\Quote;
16+
use Magento\Quote\Api\CartManagementInterface;
17+
use Magento\Quote\Model\CreateEmptyCartForCustomer;
18+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
19+
use Magento\Quote\Model\QuoteIdMaskFactory;
20+
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResourceModel;
21+
use Closure;
22+
23+
/**
24+
* Get customer cart or create empty cart. Ensure mask_id is created
25+
*/
26+
readonly class CustomerEmptyCartResolver
27+
{
28+
/**
29+
* @param CartManagementInterface $cartManagement
30+
* @param CreateEmptyCartForCustomer $createEmptyCartForCustomer
31+
* @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
32+
* @param QuoteIdMaskFactory $quoteIdMaskFactory
33+
* @param QuoteIdMaskResourceModel $quoteIdMaskResourceModel
34+
*/
35+
public function __construct(
36+
private CartManagementInterface $cartManagement,
37+
private CreateEmptyCartForCustomer $createEmptyCartForCustomer,
38+
private QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId,
39+
private QuoteIdMaskFactory $quoteIdMaskFactory,
40+
private QuoteIdMaskResourceModel $quoteIdMaskResourceModel
41+
){
42+
}
43+
44+
/**
45+
* Get customer cart by customer id with predefined masked quote id
46+
*
47+
* @param CustomerCartResolver $subject
48+
* @param Closure $proceed
49+
* @param int $customerId
50+
* @return Quote
51+
* @throws NoSuchEntityException
52+
* @throws LocalizedException
53+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
54+
*/
55+
public function aroundResolve(
56+
CustomerCartResolver $subject,
57+
Closure $proceed,
58+
int $customerId
59+
): Quote {
60+
try {
61+
/** @var Quote $cart */
62+
$cart = $this->cartManagement->getCartForCustomer($customerId);
63+
} catch (NoSuchEntityException $e) {
64+
$this->createEmptyCartForCustomer->execute($customerId);
65+
$cart = $this->cartManagement->getCartForCustomer($customerId);
66+
}
67+
try {
68+
$this->ensureQuoteMaskIdExist((int)$cart->getId());
69+
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
70+
} catch (AlreadyExistsException $e) {
71+
// do nothing, we already have masked id
72+
}
73+
74+
return $cart;
75+
}
76+
77+
/**
78+
* Create masked id for customer's active quote if it's not exists
79+
*
80+
* @param int $quoteId
81+
* @return void
82+
* @throws AlreadyExistsException
83+
*/
84+
private function ensureQuoteMaskIdExist(int $quoteId): void
85+
{
86+
try {
87+
$maskedId = $this->quoteIdToMaskedQuoteId->execute($quoteId);
88+
} catch (NoSuchEntityException $e) {
89+
$maskedId = '';
90+
}
91+
if ($maskedId === '') {
92+
$quoteIdMask = $this->quoteIdMaskFactory->create();
93+
$quoteIdMask->setQuoteId($quoteId);
94+
$this->quoteIdMaskResourceModel->save($quoteIdMask);
95+
}
96+
}
97+
}

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

+4
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\Cart\CustomerCartResolver">
83+
<plugin name="create_customer_cart"
84+
type="Magento\QuoteGraphQl\Plugin\Model\Cart\CustomerEmptyCartResolver" />
85+
</type>
8286
</config>

0 commit comments

Comments
 (0)