Skip to content

Commit ec98d13

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

File tree

2 files changed

+7
-101
lines changed

2 files changed

+7
-101
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,16 @@ public function __construct(
5959
public function execute(Quote $cart, array $cartItems): void
6060
{
6161
$lockName = 'cart_processing_lock_' . $cart->getId();
62+
$needToRefreshCache = false;
6263
while ($this->lockManager->isLocked($lockName)) {
6364
// wait till other process working with the same cart complete
64-
usleep(rand (300, 600));
65+
usleep(rand(100, 600));
66+
$needToRefreshCache = true;
67+
}
68+
$this->lockManager->lock($lockName, 1);
69+
if ($needToRefreshCache) {
70+
$this->refreshCartCache($cart);
6571
}
66-
$this->lockManager->lock($lockName);
67-
$this->refreshCartCache($cart);
6872
foreach ($cartItems as $cartItemData) {
6973
$this->addProductToCart->execute($cart, $cartItemData);
7074
}

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

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,8 @@
99

1010
use Exception;
1111
use Magento\Catalog\Api\ProductRepositoryInterface;
12-
use Magento\Catalog\Model\Product;
13-
use Magento\Framework\DataObject;
14-
use Magento\Quote\Model\Quote\Item;
15-
use Magento\Catalog\Model\Product\Type\AbstractType;
1612
use Magento\Framework\Model\Context;
1713
use Magento\Framework\Event\ManagerInterface;
18-
use Magento\Framework\Phrase;
19-
use Magento\Framework\Exception\LocalizedException;
2014
use Magento\Framework\Exception\NoSuchEntityException;
2115
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
2216
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
@@ -90,13 +84,6 @@ public function execute(Quote $cart, array $cartItemData): void
9084

9185
try {
9286
$result = $cart->addProduct($product, $this->buyRequestBuilder->build($cartItemData));
93-
/*
94-
$result = $this->addProductToCartWithConcurrency(
95-
$cart,
96-
$product,
97-
$this->buyRequestBuilder->build($cartItemData)
98-
);
99-
*/
10087
} catch (Exception $e) {
10188
throw new GraphQlInputException(
10289
__(
@@ -134,89 +121,4 @@ private function extractSku(array $cartItemData): string
134121
}
135122
return (string)$cartItemData['data']['sku'];
136123
}
137-
138-
/**
139-
* @param Quote $cart
140-
* @param Product $product
141-
* @param DataObject $request
142-
* @return Item
143-
* @throws LocalizedException
144-
*/
145-
private function addProductToCartWithConcurrency(Quote $cart, Product $product, DataObject $request) : Item
146-
{
147-
if (!$product->isSalable()) {
148-
throw new LocalizedException(
149-
__('Product that you are trying to add is not available.')
150-
);
151-
}
152-
$cartCandidates = $product->getTypeInstance()->prepareForCartAdvanced(
153-
$request,
154-
$product,
155-
AbstractType::PROCESS_MODE_FULL
156-
);
157-
if (is_string($cartCandidates) || $cartCandidates instanceof Phrase) {
158-
throw new LocalizedException((string)$cartCandidates);
159-
}
160-
if (!is_array($cartCandidates)) {
161-
$cartCandidates = [$cartCandidates];
162-
}
163-
$parentItem = null;
164-
$errors = [];
165-
$items = [];
166-
foreach ($cartCandidates as $candidate) {
167-
$stickWithinParent = $candidate->getParentProductId() ? $parentItem : null;
168-
$candidate->setStickWithinParent($stickWithinParent);
169-
$item = null;
170-
$itemsCollection = $cart->getItemsCollection(false);
171-
172-
foreach ($itemsCollection as $item) {
173-
if (!$item->isDeleted() && $item->representProduct($product)) {
174-
break;
175-
}
176-
}
177-
178-
if (!$item) {
179-
$item = $this->itemProcessor->init($candidate, $request);
180-
$item->setQuote($cart);
181-
$item->setOptions($candidate->getCustomOptions());
182-
$item->setProduct($candidate);
183-
$cart->addItem($item);
184-
}
185-
$items[] = $item;
186-
187-
if (!$parentItem) {
188-
$parentItem = $item;
189-
}
190-
if ($parentItem && $candidate->getParentProductId() && !$item->getParentItem()) {
191-
$item->setParentItem($parentItem);
192-
}
193-
194-
$this->itemProcessor->prepare($item, $request, $candidate);
195-
196-
if ($item->getHasError()) {
197-
$cart->deleteItem($item);
198-
foreach ($item->getMessage(false) as $message) {
199-
if (!in_array($message, $errors)) {
200-
$errors[] = $message;
201-
}
202-
}
203-
break;
204-
}
205-
206-
$itemsToUpdate = [];
207-
foreach ($cart->getItems() as $itemToUpdate) {
208-
if ($itemToUpdate->getItemId() === $item->getItemId()) {
209-
$itemsToUpdate[] = $item;
210-
} else {
211-
$itemsToUpdate[] = $itemToUpdate;
212-
}
213-
}
214-
$cart->setItems($itemsToUpdate);
215-
}
216-
if (!empty($errors)) {
217-
throw new LocalizedException(__(implode("\n", $errors)));
218-
}
219-
$this->eventManager->dispatch('sales_quote_product_add_after', ['items' => $items]);
220-
return $parentItem;
221-
}
222124
}

0 commit comments

Comments
 (0)