Skip to content

Commit 02b6903

Browse files
authored
LYNX-51: Added ability to extend definition of active cart
1 parent 29389e1 commit 02b6903

File tree

6 files changed

+201
-101
lines changed

6 files changed

+201
-101
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
14+
use Magento\Quote\Api\CartManagementInterface;
15+
use Magento\Quote\Model\Quote;
16+
17+
/**
18+
* Get cart
19+
*/
20+
class GetCartForCheckout
21+
{
22+
/**
23+
* @var CheckCartCheckoutAllowance
24+
*/
25+
private CheckCartCheckoutAllowance $checkoutAllowance;
26+
27+
/**
28+
* @var GetCartForUser
29+
*/
30+
private GetCartForUser $getCartForUser;
31+
32+
/**
33+
* @param CheckCartCheckoutAllowance $checkoutAllowance
34+
* @param GetCartForUser $getCartForUser
35+
*/
36+
public function __construct(
37+
CheckCartCheckoutAllowance $checkoutAllowance,
38+
GetCartForUser $getCartForUser
39+
) {
40+
$this->checkoutAllowance = $checkoutAllowance;
41+
$this->getCartForUser = $getCartForUser;
42+
}
43+
44+
/**
45+
* Gets the cart for the user validated and configured for guest checkout if applicable
46+
*
47+
* @param string $cartHash
48+
* @param int|null $customerId
49+
* @param int $storeId
50+
* @return Quote
51+
* @throws GraphQlAuthorizationException
52+
* @throws GraphQlInputException
53+
* @throws GraphQlNoSuchEntityException
54+
*/
55+
public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
56+
{
57+
try {
58+
$cart = $this->getCartForUser->execute($cartHash, $customerId, $storeId);
59+
} catch (NoSuchEntityException $e) {
60+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
61+
}
62+
$this->checkoutAllowance->execute($cart);
63+
64+
if (null === $customerId || 0 === $customerId) {
65+
if (!$cart->getCustomerEmail()) {
66+
throw new GraphQlInputException(__("Guest email for cart is missing."));
67+
}
68+
$cart->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);
69+
}
70+
71+
return $cart;
72+
}
73+
}

app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php

Lines changed: 13 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10-
use Magento\Framework\App\ObjectManager;
1110
use Magento\Framework\Exception\NoSuchEntityException;
1211
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1312
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1413
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
15-
use Magento\Quote\Api\CartManagementInterface;
1614
use Magento\Quote\Api\CartRepositoryInterface;
1715
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
1816
use Magento\Quote\Model\Quote;
19-
use Magento\Store\Api\StoreRepositoryInterface;
2017

2118
/**
2219
* Get cart
@@ -34,31 +31,31 @@ class GetCartForUser
3431
private $cartRepository;
3532

3633
/**
37-
* @var CheckCartCheckoutAllowance
34+
* @var IsActive
3835
*/
39-
private $checkoutAllowance;
36+
private $isActive;
4037

4138
/**
42-
* @var StoreRepositoryInterface
39+
* @var UpdateCartCurrency
4340
*/
44-
private $storeRepository;
41+
private $updateCartCurrency;
4542

4643
/**
4744
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
4845
* @param CartRepositoryInterface $cartRepository
49-
* @param CheckCartCheckoutAllowance $checkoutAllowance
50-
* @param StoreRepositoryInterface $storeRepository
46+
* @param IsActive $isActive
47+
* @param UpdateCartCurrency $updateCartCurrency
5148
*/
5249
public function __construct(
5350
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
5451
CartRepositoryInterface $cartRepository,
55-
CheckCartCheckoutAllowance $checkoutAllowance,
56-
StoreRepositoryInterface $storeRepository = null
52+
IsActive $isActive,
53+
UpdateCartCurrency $updateCartCurrency
5754
) {
5855
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
5956
$this->cartRepository = $cartRepository;
60-
$this->checkoutAllowance = $checkoutAllowance;
61-
$this->storeRepository = $storeRepository ?: ObjectManager::getInstance()->get(StoreRepositoryInterface::class);
57+
$this->isActive = $isActive;
58+
$this->updateCartCurrency = $updateCartCurrency;
6259
}
6360

6461
/**
@@ -77,26 +74,19 @@ public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
7774
{
7875
try {
7976
$cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash);
80-
} catch (NoSuchEntityException $exception) {
81-
throw new GraphQlNoSuchEntityException(
82-
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
83-
);
84-
}
85-
86-
try {
8777
/** @var Quote $cart */
8878
$cart = $this->cartRepository->get($cartId);
89-
} catch (NoSuchEntityException $e) {
79+
} catch (NoSuchEntityException $exception) {
9080
throw new GraphQlNoSuchEntityException(
9181
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
9282
);
9383
}
9484

95-
if (false === (bool)$cart->getIsActive()) {
85+
if (false === (bool)$this->isActive->execute($cart)) {
9686
throw new GraphQlNoSuchEntityException(__('The cart isn\'t active.'));
9787
}
9888

99-
$cart = $this->updateCartCurrency($cart, $storeId);
89+
$cart = $this->updateCartCurrency->execute($cart, $storeId);
10090

10191
$cartCustomerId = (int)$cart->getCustomerId();
10292

@@ -115,68 +105,4 @@ public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
115105
}
116106
return $cart;
117107
}
118-
119-
/**
120-
* Gets the cart for the user validated and configured for guest checkout if applicable
121-
*
122-
* @param string $cartHash
123-
* @param int|null $customerId
124-
* @param int $storeId
125-
* @return Quote
126-
* @throws GraphQlAuthorizationException
127-
* @throws GraphQlInputException
128-
* @throws GraphQlNoSuchEntityException
129-
*/
130-
public function getCartForCheckout(string $cartHash, ?int $customerId, int $storeId): Quote
131-
{
132-
try {
133-
$cart = $this->execute($cartHash, $customerId, $storeId);
134-
} catch (NoSuchEntityException $e) {
135-
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
136-
}
137-
$this->checkoutAllowance->execute($cart);
138-
139-
if ((null === $customerId || 0 === $customerId)) {
140-
if (!$cart->getCustomerEmail()) {
141-
throw new GraphQlInputException(__("Guest email for cart is missing."));
142-
}
143-
$cart->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);
144-
}
145-
146-
return $cart;
147-
}
148-
149-
/**
150-
* Sets cart currency based on specified store.
151-
*
152-
* @param Quote $cart
153-
* @param int $storeId
154-
* @return Quote
155-
* @throws GraphQlInputException
156-
* @throws NoSuchEntityException
157-
*/
158-
private function updateCartCurrency(Quote $cart, int $storeId): Quote
159-
{
160-
$cartStore = $this->storeRepository->getById($cart->getStoreId());
161-
$currentCartCurrencyCode = $cartStore->getCurrentCurrency()->getCode();
162-
if ((int)$cart->getStoreId() !== $storeId) {
163-
$newStore = $this->storeRepository->getById($storeId);
164-
if ($cartStore->getWebsite() !== $newStore->getWebsite()) {
165-
throw new GraphQlInputException(
166-
__('Can\'t assign cart to store in different website.')
167-
);
168-
}
169-
$cart->setStoreId($storeId);
170-
$cart->setStoreCurrencyCode($newStore->getCurrentCurrency());
171-
$cart->setQuoteCurrencyCode($newStore->getCurrentCurrency());
172-
} elseif ($cart->getQuoteCurrencyCode() !== $currentCartCurrencyCode) {
173-
$cart->setQuoteCurrencyCode($cartStore->getCurrentCurrency());
174-
} else {
175-
return $cart;
176-
}
177-
$this->cartRepository->save($cart);
178-
$cart = $this->cartRepository->get($cart->getId());
179-
180-
return $cart;
181-
}
182108
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Quote\Api\Data\CartInterface;
11+
12+
/**
13+
* Is cart active (can it be retrieved and updated). Requires for extensions that require to work with inactive cart.
14+
*/
15+
class IsActive
16+
{
17+
/**
18+
* Is cart active
19+
*
20+
* @param CartInterface $cart
21+
* @return bool
22+
*/
23+
public function execute(CartInterface $cart): bool
24+
{
25+
return (bool) $cart->getIsActive();
26+
}
27+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Quote\Api\CartRepositoryInterface;
14+
use Magento\Quote\Model\Quote;
15+
use Magento\Store\Api\StoreRepositoryInterface;
16+
17+
/**
18+
* Update currency
19+
*/
20+
class UpdateCartCurrency
21+
{
22+
/**
23+
* @var CartRepositoryInterface
24+
*/
25+
private CartRepositoryInterface $cartRepository;
26+
27+
/**
28+
* @var StoreRepositoryInterface
29+
*/
30+
private StoreRepositoryInterface $storeRepository;
31+
32+
/**
33+
* @param CartRepositoryInterface $cartRepository
34+
* @param StoreRepositoryInterface $storeRepository
35+
*/
36+
public function __construct(
37+
CartRepositoryInterface $cartRepository,
38+
StoreRepositoryInterface $storeRepository
39+
) {
40+
$this->cartRepository = $cartRepository;
41+
$this->storeRepository = $storeRepository;
42+
}
43+
44+
/**
45+
* Sets cart currency based on specified store.
46+
*
47+
* @param Quote $cart
48+
* @param int $storeId
49+
* @return Quote
50+
* @throws GraphQlInputException|NoSuchEntityException|LocalizedException
51+
*/
52+
public function execute(Quote $cart, int $storeId): Quote
53+
{
54+
$cartStore = $this->storeRepository->getById($cart->getStoreId());
55+
$currentCartCurrencyCode = $cartStore->getCurrentCurrency()->getCode();
56+
if ((int)$cart->getStoreId() !== $storeId) {
57+
$newStore = $this->storeRepository->getById($storeId);
58+
if ($cartStore->getWebsite() !== $newStore->getWebsite()) {
59+
throw new GraphQlInputException(
60+
__('Can\'t assign cart to store in different website.')
61+
);
62+
}
63+
$cart->setStoreId($storeId);
64+
$cart->setStoreCurrencyCode($newStore->getCurrentCurrency());
65+
$cart->setQuoteCurrencyCode($newStore->getCurrentCurrency());
66+
} elseif ($cart->getQuoteCurrencyCode() !== $currentCartCurrencyCode) {
67+
$cart->setQuoteCurrencyCode($cartStore->getCurrentCurrency());
68+
} else {
69+
return $cart;
70+
}
71+
$this->cartRepository->save($cart);
72+
return $this->cartRepository->get($cart->getId());
73+
}
74+
}

app/code/Magento/QuoteGraphQl/Model/Resolver/PlaceOrder.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Magento\Framework\GraphQl\Query\ResolverInterface;
1414
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1515
use Magento\GraphQl\Helper\Error\AggregateExceptionMessageFormatter;
16-
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
16+
use Magento\QuoteGraphQl\Model\Cart\GetCartForCheckout;
1717
use Magento\QuoteGraphQl\Model\Cart\PlaceOrder as PlaceOrderModel;
1818
use Magento\Sales\Api\OrderRepositoryInterface;
1919

@@ -23,9 +23,9 @@
2323
class PlaceOrder implements ResolverInterface
2424
{
2525
/**
26-
* @var GetCartForUser
26+
* @var GetCartForCheckout
2727
*/
28-
private $getCartForUser;
28+
private $getCartForCheckout;
2929

3030
/**
3131
* @var PlaceOrderModel
@@ -43,18 +43,18 @@ class PlaceOrder implements ResolverInterface
4343
private $errorMessageFormatter;
4444

4545
/**
46-
* @param GetCartForUser $getCartForUser
46+
* @param GetCartForCheckout $getCartForCheckout
4747
* @param PlaceOrderModel $placeOrder
4848
* @param OrderRepositoryInterface $orderRepository
4949
* @param AggregateExceptionMessageFormatter $errorMessageFormatter
5050
*/
5151
public function __construct(
52-
GetCartForUser $getCartForUser,
52+
GetCartForCheckout $getCartForCheckout,
5353
PlaceOrderModel $placeOrder,
5454
OrderRepositoryInterface $orderRepository,
5555
AggregateExceptionMessageFormatter $errorMessageFormatter
5656
) {
57-
$this->getCartForUser = $getCartForUser;
57+
$this->getCartForCheckout = $getCartForCheckout;
5858
$this->placeOrder = $placeOrder;
5959
$this->orderRepository = $orderRepository;
6060
$this->errorMessageFormatter = $errorMessageFormatter;
@@ -73,7 +73,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7373
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
7474

7575
try {
76-
$cart = $this->getCartForUser->getCartForCheckout($maskedCartId, $userId, $storeId);
76+
$cart = $this->getCartForCheckout->execute($maskedCartId, $userId, $storeId);
7777
$orderId = $this->placeOrder->execute($cart, $maskedCartId, $userId);
7878
$order = $this->orderRepository->get($orderId);
7979
} catch (LocalizedException $e) {

0 commit comments

Comments
 (0)