Skip to content

Commit 6084bc1

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 b2c6976 commit 6084bc1

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

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

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

1010
use Exception;
11-
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Api\Data\ProductInterface;
12+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
1213
use Magento\Framework\Exception\NoSuchEntityException;
1314
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1415
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
@@ -21,24 +22,24 @@
2122
class AddSimpleProductToCart
2223
{
2324
/**
24-
* @var ProductRepositoryInterface
25+
* @var ProductCollectionFactory
2526
*/
26-
private $productRepository;
27+
private $productCollectionFactory;
2728

2829
/**
2930
* @var BuyRequestBuilder
3031
*/
3132
private $buyRequestBuilder;
3233

3334
/**
34-
* @param ProductRepositoryInterface $productRepository
35+
* @param ProductCollectionFactory $productCollectionFactory
3536
* @param BuyRequestBuilder $buyRequestBuilder
3637
*/
3738
public function __construct(
38-
ProductRepositoryInterface $productRepository,
39-
BuyRequestBuilder $buyRequestBuilder
39+
ProductCollectionFactory $productCollectionFactory,
40+
BuyRequestBuilder $buyRequestBuilder
4041
) {
41-
$this->productRepository = $productRepository;
42+
$this->productCollectionFactory = $productCollectionFactory;
4243
$this->buyRequestBuilder = $buyRequestBuilder;
4344
}
4445

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

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->getStoreId(), $product->getStoreIds())) {
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()) {
6466
throw new GraphQlNoSuchEntityException(__('Could not find a product with SKU "%sku"', ['sku' => $sku]));
6567
}
6668

0 commit comments

Comments
 (0)