Skip to content

Commit ea236e7

Browse files
authored
Merge pull request #6963 from magento-l3/MC-42652
MC-42652: GraphQL - Expected behavior of add product to cart when SKU already exists
2 parents 055e4ed + ce7fde8 commit ea236e7

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace Magento\QuoteGraphQl\Model\Cart;
99

1010
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
11-
use Magento\Framework\Message\MessageInterface;
1211
use Magento\Quote\Api\CartRepositoryInterface;
1312
use Magento\Quote\Model\Quote;
1413

@@ -53,7 +52,6 @@ public function execute(Quote $cart, array $cartItems): void
5352
foreach ($cartItems as $cartItemData) {
5453
$this->addProductToCart->execute($cart, $cartItemData);
5554
}
56-
5755
$this->cartRepository->save($cart);
5856
}
5957
}

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1414
use Magento\QuoteGraphQl\Model\Cart\AddProductsToCart;
1515
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
16+
use Magento\Framework\Lock\LockManagerInterface;
1617

1718
/**
1819
* Add simple products to cart GraphQl resolver
@@ -30,16 +31,24 @@ class AddSimpleProductsToCart implements ResolverInterface
3031
*/
3132
private $addProductsToCart;
3233

34+
/**
35+
* @var LockManagerInterface
36+
*/
37+
private $lockManager;
38+
3339
/**
3440
* @param GetCartForUser $getCartForUser
3541
* @param AddProductsToCart $addProductsToCart
42+
* @param LockManagerInterface $lockManager
3643
*/
3744
public function __construct(
3845
GetCartForUser $getCartForUser,
39-
AddProductsToCart $addProductsToCart
46+
AddProductsToCart $addProductsToCart,
47+
LockManagerInterface $lockManager
4048
) {
4149
$this->getCartForUser = $getCartForUser;
4250
$this->addProductsToCart = $addProductsToCart;
51+
$this->lockManager = $lockManager;
4352
}
4453

4554
/**
@@ -58,12 +67,20 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
5867
throw new GraphQlInputException(__('Required parameter "cart_items" is missing'));
5968
}
6069
$cartItems = $args['input']['cart_items'];
61-
6270
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
71+
72+
$lockName = 'cart_processing_lock_' . $maskedCartId;
73+
while ($this->lockManager->isLocked($lockName)) {
74+
// wait till other process working with the same cart complete
75+
usleep(rand(100, 600));
76+
}
77+
$this->lockManager->lock($lockName, 1);
78+
6379
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
6480
$this->addProductsToCart->execute($cart, $cartItems);
65-
6681
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
82+
83+
$this->lockManager->unlock($lockName);
6784
return [
6885
'cart' => [
6986
'model' => $cart,

0 commit comments

Comments
 (0)