Skip to content

Commit c26432f

Browse files
author
Prabhu Ram
committed
PWA-1655: [bug]: Broken Add to Cart for logged in users
- refactor
1 parent d0592c0 commit c26432f

File tree

4 files changed

+111
-37
lines changed

4 files changed

+111
-37
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\MergeCarts;
9+
10+
11+
use Magento\CatalogInventory\Api\StockRegistryInterface;
12+
use Magento\Framework\Exception\CouldNotSaveException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Quote\Api\CartItemRepositoryInterface;
15+
use Magento\Quote\Api\Data\CartInterface;
16+
use Magento\Quote\Api\Data\CartItemInterface;
17+
18+
class CartQuantityValidator implements CartQuantityValidatorInterface
19+
{
20+
/**
21+
* @var CartItemRepositoryInterface
22+
*/
23+
private $cartItemRepository;
24+
25+
/**
26+
* @var StockRegistryInterface
27+
*/
28+
private $stockRegistry;
29+
30+
/**
31+
* @param CartItemRepositoryInterface $cartItemRepository
32+
* @param StockRegistryInterface $stockRegistry
33+
*/
34+
public function __construct(
35+
CartItemRepositoryInterface $cartItemRepository,
36+
StockRegistryInterface $stockRegistry
37+
) {
38+
$this->cartItemRepository = $cartItemRepository;
39+
$this->stockRegistry = $stockRegistry;
40+
}
41+
42+
/**
43+
* Validate combined cart quantities to make sure they are within available stock
44+
*
45+
* @param CartInterface $customerCart
46+
* @param CartInterface $guestCart
47+
* @return bool
48+
*/
49+
public function validateFinalCartQuantities(CartInterface $customerCart, CartInterface $guestCart): bool
50+
{
51+
$modified = false;
52+
/** @var CartItemInterface $guestCartItem */
53+
foreach ($guestCart->getAllVisibleItems() as $guestCartItem) {
54+
foreach ($customerCart->getAllItems() as $customerCartItem) {
55+
if ($customerCartItem->compare($guestCartItem)) {
56+
$product = $customerCartItem->getProduct();
57+
$stockCurrentQty = $this->stockRegistry->getStockStatus(
58+
$product->getId(),
59+
$product->getStore()->getWebsiteId()
60+
)->getQty();
61+
if ($stockCurrentQty < $guestCartItem->getQty() + $customerCartItem->getQty()) {
62+
try {
63+
$this->cartItemRepository->deleteById($guestCart->getId(), $guestCartItem->getItemId());
64+
$modified = true;
65+
} catch (NoSuchEntityException $e) {
66+
continue;
67+
} catch (CouldNotSaveException $e) {
68+
continue;
69+
}
70+
}
71+
}
72+
}
73+
}
74+
return $modified;
75+
}
76+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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\MergeCarts;
9+
10+
use Magento\Quote\Api\Data\CartInterface;
11+
12+
interface CartQuantityValidatorInterface
13+
{
14+
/**
15+
* Validate cart quantities when merging
16+
*
17+
* @param CartInterface $customerCart
18+
* @param CartInterface $guestCart
19+
* @return bool
20+
*/
21+
public function validateFinalCartQuantities(CartInterface $customerCart, CartInterface $guestCart): bool;
22+
}

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

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Magento\Quote\Model\Cart\CustomerCartResolver;
2626
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
2727
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
28+
use Magento\QuoteGraphQl\Model\Cart\MergeCarts\CartQuantityValidatorInterface;
2829

2930
/**
3031
* Merge Carts Resolver
@@ -63,6 +64,11 @@ class MergeCarts implements ResolverInterface
6364
*/
6465
private $stockRegistry;
6566

67+
/**
68+
* @var CartQuantityValidatorInterface
69+
*/
70+
private $cartQuantityValidator;
71+
6672
/**
6773
* @param GetCartForUser $getCartForUser
6874
* @param CartRepositoryInterface $cartRepository
@@ -77,7 +83,8 @@ public function __construct(
7783
CustomerCartResolver $customerCartResolver = null,
7884
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId = null,
7985
CartItemRepositoryInterface $cartItemRepository = null,
80-
StockRegistryInterface $stockRegistry = null
86+
StockRegistryInterface $stockRegistry = null,
87+
CartQuantityValidatorInterface $cartQuantityValidator = null
8188
) {
8289
$this->getCartForUser = $getCartForUser;
8390
$this->cartRepository = $cartRepository;
@@ -89,6 +96,8 @@ public function __construct(
8996
?: ObjectManager::getInstance()->get(CartItemRepositoryInterface::class);
9097
$this->stockRegistry = $stockRegistry
9198
?: ObjectManager::getInstance()->get(StockRegistryInterface::class);
99+
$this->cartQuantityValidator = $cartQuantityValidator
100+
?: ObjectManager::getInstance()->get(CartQuantityValidatorInterface::class);
92101
}
93102

94103
/**
@@ -150,7 +159,7 @@ public function resolve(
150159
$currentUserId,
151160
$storeId
152161
);
153-
if ($this->validateFinalCartQuantities($customerCart, $guestCart)) {
162+
if ($this->cartQuantityValidator->validateFinalCartQuantities($customerCart, $guestCart)) {
154163
$guestCart = $this->getCartForUser->execute(
155164
$guestMaskedCartId,
156165
null,
@@ -165,39 +174,4 @@ public function resolve(
165174
'model' => $customerCart,
166175
];
167176
}
168-
169-
/**
170-
* Validate combined cart quantities to make sure they are within available stock
171-
*
172-
* @param CartInterface $customerCart
173-
* @param CartInterface $guestCart
174-
* @return bool
175-
*/
176-
private function validateFinalCartQuantities(CartInterface $customerCart, CartInterface $guestCart)
177-
{
178-
$modified = false;
179-
/** @var CartItemInterface $guestCartItem */
180-
foreach ($guestCart->getAllVisibleItems() as $guestCartItem) {
181-
foreach ($customerCart->getAllItems() as $customerCartItem) {
182-
if ($customerCartItem->compare($guestCartItem)) {
183-
$product = $customerCartItem->getProduct();
184-
$stockCurrentQty = $this->stockRegistry->getStockStatus(
185-
$product->getId(),
186-
$product->getStore()->getWebsiteId()
187-
)->getQty();
188-
if ($stockCurrentQty < $guestCartItem->getQty() + $customerCartItem->getQty()) {
189-
try {
190-
$this->cartItemRepository->deleteById($guestCart->getId(), $guestCartItem->getItemId());
191-
$modified = true;
192-
} catch (NoSuchEntityException $e) {
193-
continue;
194-
} catch (CouldNotSaveException $e) {
195-
continue;
196-
}
197-
}
198-
}
199-
}
200-
}
201-
return $modified;
202-
}
203177
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
type="Magento\QuoteGraphQl\Model\Cart\SetShippingAddressesOnCart"/>
1111
<preference for="Magento\QuoteGraphQl\Model\Cart\SetShippingMethodsOnCartInterface"
1212
type="Magento\QuoteGraphQl\Model\Cart\SetShippingMethodsOnCart"/>
13+
<preference for="Magento\QuoteGraphQl\Model\Cart\MergeCarts\CartQuantityValidatorInterface"
14+
type="Magento\QuoteGraphQl\Model\Cart\MergeCarts\CartQuantityValidator"/>
1315
<type name="Magento\QuoteGraphQl\Model\Cart\BuyRequest\BuyRequestBuilder">
1416
<arguments>
1517
<argument name="providers" xsi:type="array">

0 commit comments

Comments
 (0)