|
7 | 7 |
|
8 | 8 | namespace Magento\Quote\Model\Cart; |
9 | 9 |
|
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
10 | 11 | use Magento\Framework\Exception\NoSuchEntityException; |
11 | 12 | use Magento\Quote\Api\CartRepositoryInterface; |
12 | 13 | use Magento\Quote\Model\Cart\BuyRequest\BuyRequestBuilder; |
@@ -46,25 +47,33 @@ class AddProductsToCart |
46 | 47 | */ |
47 | 48 | private $error; |
48 | 49 |
|
| 50 | + /** |
| 51 | + * @var ProductRepositoryInterface |
| 52 | + */ |
| 53 | + private $productRepository; |
| 54 | + |
49 | 55 | /** |
50 | 56 | * @param CartRepositoryInterface $cartRepository |
51 | 57 | * @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId |
52 | 58 | * @param BuyRequestBuilder $requestBuilder |
53 | 59 | * @param ProductReaderInterface $productReader |
54 | 60 | * @param AddProductsToCartError $addProductsToCartError |
| 61 | + * @param ProductRepositoryInterface $productRepository |
55 | 62 | */ |
56 | 63 | public function __construct( |
57 | 64 | CartRepositoryInterface $cartRepository, |
58 | 65 | MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId, |
59 | 66 | BuyRequestBuilder $requestBuilder, |
60 | 67 | ProductReaderInterface $productReader, |
61 | | - AddProductsToCartError $addProductsToCartError |
| 68 | + AddProductsToCartError $addProductsToCartError, |
| 69 | + ProductRepositoryInterface $productRepository |
62 | 70 | ) { |
63 | 71 | $this->cartRepository = $cartRepository; |
64 | 72 | $this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId; |
65 | 73 | $this->requestBuilder = $requestBuilder; |
66 | 74 | $this->productReader = $productReader; |
67 | 75 | $this->error = $addProductsToCartError; |
| 76 | + $this->productRepository = $productRepository; |
68 | 77 | } |
69 | 78 |
|
70 | 79 | /** |
@@ -134,7 +143,6 @@ function ($item) { |
134 | 143 | }, |
135 | 144 | $cartItems |
136 | 145 | ); |
137 | | - $this->productReader->loadProducts($skus, $cart->getStoreId()); |
138 | 146 | foreach ($cartItems as $cartItemPosition => $cartItem) { |
139 | 147 | $errors = $this->addItemToCart($cart, $cartItem, $cartItemPosition); |
140 | 148 | if ($errors) { |
@@ -165,7 +173,11 @@ private function addItemToCart(Quote $cart, Data\CartItem $cartItem, int $cartIt |
165 | 173 | $cartItemPosition |
166 | 174 | ); |
167 | 175 | } else { |
168 | | - $product = $this->productReader->getProductBySku($sku); |
| 176 | + try { |
| 177 | + $product = clone $this->productRepository->get($sku, false, $cart->getStoreId()); |
| 178 | + } catch (NoSuchEntityException $e) { |
| 179 | + $product = null; |
| 180 | + } |
169 | 181 | if (!$product || !$product->isSaleable() || !$product->isAvailable()) { |
170 | 182 | $errors[] = $this->error->create( |
171 | 183 | __('Could not find a product with SKU "%sku"', ['sku' => $sku])->render(), |
|
0 commit comments