Skip to content

Commit fb04ed9

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 3c5ff49 commit fb04ed9

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

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

Lines changed: 22 additions & 9 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\Api\ProductRepositoryInterface;
10+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Quote\Api\CartRepositoryInterface;
1313
use Magento\Quote\Api\Data\CartInterface;
@@ -16,6 +16,7 @@
1616
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
1717
use Magento\Quote\Model\Quote;
1818
use Magento\Framework\Message\MessageInterface;
19+
use Magento\Store\Model\StoreManagerInterface;
1920

2021
/**
2122
* Unified approach to add products to the Shopping Cart.
@@ -47,9 +48,14 @@ class AddProductsToCart
4748
];
4849

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

5460
/**
5561
* @var array
@@ -72,18 +78,21 @@ class AddProductsToCart
7278
private $requestBuilder;
7379

7480
/**
75-
* @param ProductRepositoryInterface $productRepository
81+
* @param CollectionFactory $productCollectionFactory
82+
* @param StoreManagerInterface $storeManager
7683
* @param CartRepositoryInterface $cartRepository
7784
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
7885
* @param BuyRequestBuilder $requestBuilder
7986
*/
8087
public function __construct(
81-
ProductRepositoryInterface $productRepository,
88+
CollectionFactory $productCollectionFactory,
89+
StoreManagerInterface $storeManager,
8290
CartRepositoryInterface $cartRepository,
8391
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
8492
BuyRequestBuilder $requestBuilder
8593
) {
86-
$this->productRepository = $productRepository;
94+
$this->productCollectionFactory = $productCollectionFactory;
95+
$this->storeManager = $storeManager;
8796
$this->cartRepository = $cartRepository;
8897
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
8998
$this->requestBuilder = $requestBuilder;
@@ -140,9 +149,13 @@ private function addItemToCart(CartInterface $cart, Data\CartItem $cartItem, int
140149
return;
141150
}
142151

143-
try {
144-
$product = $this->productRepository->get($sku, false, null, true);
145-
} catch (NoSuchEntityException $e) {
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()) {
146159
$this->addError(
147160
__('Could not find a product with SKU "%sku"', ['sku' => $sku])->render(),
148161
$cartItemPosition

0 commit comments

Comments
 (0)