Skip to content

Commit 2dd5c54

Browse files
committed
MC-42902: Duplicate SKU on same order
- Lock mechanism has been added to avoid quote item duplication on concurrent requests
1 parent 2c94da6 commit 2dd5c54

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1212
use Magento\Framework\GraphQl\Query\ResolverInterface;
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\Framework\Lock\LockManagerInterface;
1415
use Magento\GraphQl\Model\Query\ContextInterface;
1516
use Magento\Quote\Model\Cart\AddProductsToCart as AddProductsToCartService;
1617
use Magento\Quote\Model\Cart\Data\AddProductsToCartOutput;
@@ -41,19 +42,27 @@ class AddProductsToCart implements ResolverInterface
4142
*/
4243
private $itemDataProcessor;
4344

45+
/**
46+
* @var LockManagerInterface
47+
*/
48+
private $lockManager;
49+
4450
/**
4551
* @param GetCartForUser $getCartForUser
4652
* @param AddProductsToCartService $addProductsToCart
47-
* @param ItemDataProcessorInterface $itemDataProcessor
53+
* @param ItemDataProcessorInterface $itemDataProcessor
54+
* @param LockManagerInterface $lockManager
4855
*/
4956
public function __construct(
5057
GetCartForUser $getCartForUser,
5158
AddProductsToCartService $addProductsToCart,
52-
ItemDataProcessorInterface $itemDataProcessor
59+
ItemDataProcessorInterface $itemDataProcessor,
60+
LockManagerInterface $lockManager
5361
) {
5462
$this->getCartForUser = $getCartForUser;
5563
$this->addProductsToCartService = $addProductsToCart;
5664
$this->itemDataProcessor = $itemDataProcessor;
65+
$this->lockManager = $lockManager;
5766
}
5867

5968
/**
@@ -72,6 +81,12 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7281
$maskedCartId = $args['cartId'];
7382
$cartItemsData = $args['cartItems'];
7483
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
84+
$lockName = 'cart_processing_lock_' . $maskedCartId;
85+
while ($this->lockManager->isLocked($lockName)) {
86+
// wait till other process working with the same cart complete
87+
usleep(rand(100, 600));
88+
}
89+
$this->lockManager->lock($lockName, 1);
7590

7691
// Shopping Cart validation
7792
$this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
@@ -86,6 +101,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
86101

87102
/** @var AddProductsToCartOutput $addProductsToCartOutput */
88103
$addProductsToCartOutput = $this->addProductsToCartService->execute($maskedCartId, $cartItems);
104+
$this->lockManager->unlock($lockName);
89105

90106
return [
91107
'cart' => [

0 commit comments

Comments
 (0)