1313use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
1414use Magento \QuoteGraphQl \Model \Cart \AddProductsToCart ;
1515use 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