Skip to content

Commit 83a51bd

Browse files
Roman HaninRoman Hanin
authored andcommitted
B2B-2243: Optimize addProductsToCart operations
1 parent 6ee48e4 commit 83a51bd

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,15 @@ private function addItemToCart(Quote $cart, Data\CartItem $cartItem, int $cartIt
191191
__('Could not find a product with SKU "%sku"', ['sku' => $sku])->render(),
192192
$cartItemPosition
193193
);
194-
}
195-
try {
196-
$result = $cart->addProduct($product, $this->requestBuilder->build($cartItem));
197-
} catch (\Throwable $e) {
198-
$errors[] = $this->createError(
199-
__($e->getMessage())->render(),
200-
$cartItemPosition
201-
);
194+
} else {
195+
try {
196+
$result = $cart->addProduct($product, $this->requestBuilder->build($cartItem));
197+
} catch (\Throwable $e) {
198+
$errors[] = $this->createError(
199+
__($e->getMessage())->render(),
200+
$cartItemPosition
201+
);
202+
}
202203
}
203204

204205
if (is_string($result)) {

app/code/Magento/Quote/Model/Cart/ProductReader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __construct(
5050
}
5151

5252
/**
53-
* @inheirtDoc
53+
* @inheritDoc
5454
*/
5555
public function loadProducts(array $skus, int $storeId): void
5656
{
@@ -68,7 +68,7 @@ public function loadProducts(array $skus, int $storeId): void
6868
}
6969

7070
/**
71-
* @inheirtdoc
71+
* @inheritDoc
7272
*/
7373
public function getProductBySku(string $sku) : ?ProductInterface
7474
{

app/code/Magento/Quote/Model/ResourceModel/Quote/Item/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ private function removeItemsWithAbsentProducts(): void
382382

383383
$productCollection = $this->_productCollectionFactory->create()->addIdFilter($this->_productIds);
384384
$existingProductsIds = $productCollection->getAllIds();
385-
$absentProductsIds = array_diff($this->_productIds, $existingProductsIds);
385+
$absentProductsIds = array_unique(array_diff($this->_productIds, $existingProductsIds));
386386
// Remove not existing products from items collection
387387
if (!empty($absentProductsIds)) {
388388
foreach ($absentProductsIds as $productIdToExclude) {

0 commit comments

Comments
 (0)