Skip to content

Commit d859e0c

Browse files
committed
MC-42781: [Magento Cloud][Premier Support] Graphql request can add product to cart that is NOT assigned to a website of a multi site store
1 parent fb04ed9 commit d859e0c

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Magento\Quote\Model\Cart;
99

10-
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Quote\Api\CartRepositoryInterface;
1313
use Magento\Quote\Api\Data\CartInterface;
@@ -16,7 +16,6 @@
1616
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
1717
use Magento\Quote\Model\Quote;
1818
use Magento\Framework\Message\MessageInterface;
19-
use Magento\Store\Model\StoreManagerInterface;
2019

2120
/**
2221
* Unified approach to add products to the Shopping Cart.
@@ -48,14 +47,9 @@ class AddProductsToCart
4847
];
4948

5049
/**
51-
* @var CollectionFactory
50+
* @var ProductRepositoryInterface
5251
*/
53-
private $productCollectionFactory;
54-
55-
/**
56-
* @var StoreManagerInterface
57-
*/
58-
private $storeManager;
52+
private $productRepository;
5953

6054
/**
6155
* @var array
@@ -78,21 +72,18 @@ class AddProductsToCart
7872
private $requestBuilder;
7973

8074
/**
81-
* @param CollectionFactory $productCollectionFactory
82-
* @param StoreManagerInterface $storeManager
75+
* @param ProductRepositoryInterface $productRepository
8376
* @param CartRepositoryInterface $cartRepository
8477
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
8578
* @param BuyRequestBuilder $requestBuilder
8679
*/
8780
public function __construct(
88-
CollectionFactory $productCollectionFactory,
89-
StoreManagerInterface $storeManager,
81+
ProductRepositoryInterface $productRepository,
9082
CartRepositoryInterface $cartRepository,
9183
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
9284
BuyRequestBuilder $requestBuilder
9385
) {
94-
$this->productCollectionFactory = $productCollectionFactory;
95-
$this->storeManager = $storeManager;
86+
$this->productRepository = $productRepository;
9687
$this->cartRepository = $cartRepository;
9788
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
9889
$this->requestBuilder = $requestBuilder;
@@ -149,13 +140,18 @@ private function addItemToCart(CartInterface $cart, Data\CartItem $cartItem, int
149140
return;
150141
}
151142

152-
$store = $this->storeManager->getStore($cart->getStoreId());
153-
$productCollection = $this->productCollectionFactory->create()
154-
->addAttributeToFilter('sku', $sku)
155-
->addWebsiteFilter([$store->getWebsiteId()])
156-
->load();
157-
$product = $productCollection->getFirstItem();
158-
if (!$product->getId()) {
143+
try {
144+
$product = $this->productRepository->get($sku, false, null, true);
145+
} catch (NoSuchEntityException $e) {
146+
$this->addError(
147+
__('Could not find a product with SKU "%sku"', ['sku' => $sku])->render(),
148+
$cartItemPosition
149+
);
150+
151+
return;
152+
}
153+
154+
if (!in_array($cart->getStoreId(), $product->getStoreIds())) {
159155
$this->addError(
160156
__('Could not find a product with SKU "%sku"', ['sku' => $sku])->render(),
161157
$cartItemPosition

0 commit comments

Comments
 (0)