Skip to content

Commit 7ae0429

Browse files
committed
MC-42970: [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 6084bc1 commit 7ae0429

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
namespace Magento\QuoteGraphQl\Model\Cart;
99

1010
use Exception;
11-
use Magento\Catalog\Api\Data\ProductInterface;
12-
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
1312
use Magento\Framework\Exception\NoSuchEntityException;
1413
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1514
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
@@ -22,24 +21,24 @@
2221
class AddSimpleProductToCart
2322
{
2423
/**
25-
* @var ProductCollectionFactory
24+
* @var ProductRepositoryInterface
2625
*/
27-
private $productCollectionFactory;
26+
private $productRepository;
2827

2928
/**
3029
* @var BuyRequestBuilder
3130
*/
3231
private $buyRequestBuilder;
3332

3433
/**
35-
* @param ProductCollectionFactory $productCollectionFactory
34+
* @param ProductRepositoryInterface $productRepository
3635
* @param BuyRequestBuilder $buyRequestBuilder
3736
*/
3837
public function __construct(
39-
ProductCollectionFactory $productCollectionFactory,
40-
BuyRequestBuilder $buyRequestBuilder
38+
ProductRepositoryInterface $productRepository,
39+
BuyRequestBuilder $buyRequestBuilder
4140
) {
42-
$this->productCollectionFactory = $productCollectionFactory;
41+
$this->productRepository = $productRepository;
4342
$this->buyRequestBuilder = $buyRequestBuilder;
4443
}
4544

@@ -56,13 +55,12 @@ public function execute(Quote $cart, array $cartItemData): void
5655
$cartItemData['model'] = $cart;
5756
$sku = $this->extractSku($cartItemData);
5857

59-
$productCollection = $this->productCollectionFactory->create()
60-
->addAttributeToFilter(ProductInterface::SKU, $sku)
61-
->addWebsiteFilter([$cart->getStore()->getWebsiteId()])
62-
->load();
63-
/** @var ProductInterface $product */
64-
$product = $productCollection->getFirstItem();
65-
if (!$product->getId()) {
58+
try {
59+
$product = $this->productRepository->get($sku, false, null, true);
60+
} catch (NoSuchEntityException $e) {
61+
throw new GraphQlNoSuchEntityException(__('Could not find a product with SKU "%sku"', ['sku' => $sku]));
62+
}
63+
if (!in_array($cart->getStore()->getWebsiteId(), $product->getWebsiteIds())) {
6664
throw new GraphQlNoSuchEntityException(__('Could not find a product with SKU "%sku"', ['sku' => $sku]));
6765
}
6866

0 commit comments

Comments
 (0)