|
8 | 8 | namespace Magento\Quote\Model\Cart;
|
9 | 9 |
|
10 | 10 | use Magento\Catalog\Api\ProductRepositoryInterface;
|
| 11 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 12 | +use Magento\Framework\App\ObjectManager; |
11 | 13 | use Magento\Framework\Exception\NoSuchEntityException;
|
12 | 14 | use Magento\Quote\Api\CartRepositoryInterface;
|
13 | 15 | use Magento\Quote\Model\Cart\BuyRequest\BuyRequestBuilder;
|
@@ -65,22 +67,32 @@ class AddProductsToCart
|
65 | 67 | */
|
66 | 68 | private $requestBuilder;
|
67 | 69 |
|
| 70 | + /** |
| 71 | + * @var SearchCriteriaBuilder |
| 72 | + */ |
| 73 | + private $searchCriteriaBuilder; |
| 74 | + |
68 | 75 | /**
|
69 | 76 | * @param ProductRepositoryInterface $productRepository
|
70 | 77 | * @param CartRepositoryInterface $cartRepository
|
71 | 78 | * @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
|
72 | 79 | * @param BuyRequestBuilder $requestBuilder
|
| 80 | + * @param SearchCriteriaBuilder|null $searchCriteriaBuilder |
73 | 81 | */
|
74 | 82 | public function __construct(
|
75 | 83 | ProductRepositoryInterface $productRepository,
|
76 | 84 | CartRepositoryInterface $cartRepository,
|
77 | 85 | MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
|
78 |
| - BuyRequestBuilder $requestBuilder |
| 86 | + BuyRequestBuilder $requestBuilder, |
| 87 | + SearchCriteriaBuilder $searchCriteriaBuilder = null |
79 | 88 | ) {
|
80 | 89 | $this->productRepository = $productRepository;
|
81 | 90 | $this->cartRepository = $cartRepository;
|
82 | 91 | $this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
|
83 | 92 | $this->requestBuilder = $requestBuilder;
|
| 93 | + $this->searchCriteriaBuilder = $searchCriteriaBuilder ?: ObjectManager::getInstance()->get( |
| 94 | + SearchCriteriaBuilder::class |
| 95 | + ); |
84 | 96 | }
|
85 | 97 |
|
86 | 98 | /**
|
@@ -145,6 +157,16 @@ public function execute(string $maskedCartId, array $cartItems): AddProductsToCa
|
145 | 157 | public function addItemsToCart(Quote $cart, array $cartItems): array
|
146 | 158 | {
|
147 | 159 | $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(); |
148 | 170 |
|
149 | 171 | foreach ($cartItems as $cartItemPosition => $cartItem) {
|
150 | 172 | $errors = $this->addItemToCart($cart, $cartItem, $cartItemPosition);
|
@@ -178,7 +200,7 @@ private function addItemToCart(Quote $cart, Data\CartItem $cartItem, int $cartIt
|
178 | 200 | );
|
179 | 201 | } else {
|
180 | 202 | try {
|
181 |
| - $product = $this->productRepository->get($sku, false, null, true); |
| 203 | + $product = $this->productRepository->get($sku, false, $cart->getStoreId(), false); |
182 | 204 | } catch (NoSuchEntityException $e) {
|
183 | 205 | $errors[] = $this->createError(
|
184 | 206 | __('Could not find a product with SKU "%sku"', ['sku' => $sku])->render(),
|
|
0 commit comments