Skip to content

Commit 34c9d4f

Browse files
author
Oleksandr Iegorov
committed
MC-42652: GraphQL - Expected behavior of add product to cart when SKU already exists
1 parent 19cc4b9 commit 34c9d4f

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1111
use Magento\Quote\Api\CartRepositoryInterface;
1212
use Magento\Quote\Model\Quote;
13-
use Magento\Framework\App\CacheInterface;
13+
use Magento\Framework\Lock\LockManagerInterface;
1414

1515
/**
1616
* Adding products to cart using GraphQL
@@ -28,23 +28,23 @@ class AddProductsToCart
2828
private $addProductToCart;
2929

3030
/**
31-
* @var CacheInterface
31+
* @var LockManagerInterface
3232
*/
33-
private $cache;
33+
private $lockManager;
3434

3535
/**
3636
* @param CartRepositoryInterface $cartRepository
3737
* @param AddSimpleProductToCart $addProductToCart
38-
* @param CacheInterface $cache
38+
* @param LockManagerInterface $lockManager
3939
*/
4040
public function __construct(
4141
CartRepositoryInterface $cartRepository,
4242
AddSimpleProductToCart $addProductToCart,
43-
CacheInterface $cache
43+
LockManagerInterface $lockManager
4444
) {
4545
$this->cartRepository = $cartRepository;
4646
$this->addProductToCart = $addProductToCart;
47-
$this->cache = $cache;
47+
$this->lockManager = $lockManager;
4848
}
4949

5050
/**
@@ -58,16 +58,32 @@ public function __construct(
5858
*/
5959
public function execute(Quote $cart, array $cartItems): void
6060
{
61-
$ck = 'cart_processing_mutex_' . $cart->getId();
62-
while ($this->cache->load($ck) === '1') {
61+
$lockName = 'cart_processing_lock_' . $cart->getId();
62+
while ($this->lockManager->isLocked($lockName)) {
6363
// wait till other process working with the same cart complete
6464
usleep(rand (300, 600));
6565
}
66-
$this->cache->save('1', $ck, [], 1);
66+
$this->lockManager->lock($lockName);
67+
$this->refreshCartCache($cart);
6768
foreach ($cartItems as $cartItemData) {
6869
$this->addProductToCart->execute($cart, $cartItemData);
6970
}
7071
$this->cartRepository->save($cart);
71-
$this->cache->remove($ck);
72+
$this->lockManager->unlock($lockName);
73+
}
74+
75+
/**
76+
* Refresh cart collection cache
77+
*
78+
* @param Quote $cart
79+
*/
80+
private function refreshCartCache(Quote $cart) : void
81+
{
82+
$items = [];
83+
$collection = $cart->getItemsCollection(false);
84+
foreach ($collection as $item) {
85+
$items[] = $item;
86+
}
87+
$cart->setItems($items);
7288
}
7389
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,6 @@ public function execute(Quote $cart, array $cartItemData): void
8989
}
9090

9191
try {
92-
$items = [];
93-
$collection = $cart->getItemsCollection(false);
94-
foreach ($collection as $item) {
95-
$items[] = $item;
96-
}
97-
$cart->setItems($items);
9892
$result = $cart->addProduct($product, $this->buyRequestBuilder->build($cartItemData));
9993
/*
10094
$result = $this->addProductToCartWithConcurrency(

0 commit comments

Comments
 (0)