Skip to content

Commit 7b1feab

Browse files
committed
Merge remote-tracking branch 'arcticfoxes/B2B-2331' into B2B-2379
2 parents 90601b7 + 8473afc commit 7b1feab

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

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

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

1010
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\Api\SearchCriteriaBuilder;
12+
use Magento\Framework\App\ObjectManager;
1113
use Magento\Framework\Exception\NoSuchEntityException;
1214
use Magento\Quote\Api\CartRepositoryInterface;
1315
use Magento\Quote\Model\Cart\BuyRequest\BuyRequestBuilder;
@@ -65,22 +67,32 @@ class AddProductsToCart
6567
*/
6668
private $requestBuilder;
6769

70+
/**
71+
* @var SearchCriteriaBuilder
72+
*/
73+
private $searchCriteriaBuilder;
74+
6875
/**
6976
* @param ProductRepositoryInterface $productRepository
7077
* @param CartRepositoryInterface $cartRepository
7178
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
7279
* @param BuyRequestBuilder $requestBuilder
80+
* @param SearchCriteriaBuilder|null $searchCriteriaBuilder
7381
*/
7482
public function __construct(
7583
ProductRepositoryInterface $productRepository,
7684
CartRepositoryInterface $cartRepository,
7785
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
78-
BuyRequestBuilder $requestBuilder
86+
BuyRequestBuilder $requestBuilder,
87+
SearchCriteriaBuilder $searchCriteriaBuilder = null
7988
) {
8089
$this->productRepository = $productRepository;
8190
$this->cartRepository = $cartRepository;
8291
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
8392
$this->requestBuilder = $requestBuilder;
93+
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: ObjectManager::getInstance()->get(
94+
SearchCriteriaBuilder::class
95+
);
8496
}
8597

8698
/**
@@ -145,6 +157,16 @@ public function execute(string $maskedCartId, array $cartItems): AddProductsToCa
145157
public function addItemsToCart(Quote $cart, array $cartItems): array
146158
{
147159
$failedCartItems = [];
160+
$cartItemSkus = \array_map(
161+
function ($item) {
162+
return $item->getSku();
163+
},
164+
$cartItems
165+
);
166+
167+
$searchCriteria = $this->searchCriteriaBuilder->addFilter('sku', $cartItemSkus, 'in')->create();
168+
// getList() call caches product models in runtime cache
169+
$this->productRepository->getList($searchCriteria)->getItems();
148170

149171
foreach ($cartItems as $cartItemPosition => $cartItem) {
150172
$errors = $this->addItemToCart($cart, $cartItem, $cartItemPosition);
@@ -178,7 +200,7 @@ private function addItemToCart(Quote $cart, Data\CartItem $cartItem, int $cartIt
178200
);
179201
} else {
180202
try {
181-
$product = $this->productRepository->get($sku, false, null, true);
203+
$product = $this->productRepository->get($sku, false, $cart->getStoreId(), false);
182204
} catch (NoSuchEntityException $e) {
183205
$errors[] = $this->createError(
184206
__('Could not find a product with SKU "%sku"', ['sku' => $sku])->render(),

0 commit comments

Comments
 (0)