Skip to content

Commit 72b9212

Browse files
committed
ACP2E-175: Internal error when adding a disabled product to cart using GraphQl
1 parent eea292a commit 72b9212

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogGraphQl\Plugin;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Model\Product\Attribute\Source\Status as ProductStatus;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\GraphQl\Model\Query\ContextFactoryInterface;
15+
16+
class AvailableProductsFilter
17+
{
18+
/**
19+
* @var ContextFactoryInterface
20+
*/
21+
private $contextFactory;
22+
23+
/**
24+
* @param ContextFactoryInterface $contextFactory
25+
*/
26+
public function __construct(ContextFactoryInterface $contextFactory)
27+
{
28+
$this->contextFactory = $contextFactory;
29+
}
30+
31+
/**
32+
* Check that product is available.
33+
*
34+
* @param ProductRepositoryInterface $subject
35+
* @param ProductInterface $result
36+
* @param string $sku
37+
* @param bool $editMode
38+
* @param int|null $storeId
39+
* @param bool $forceReload
40+
* @return ProductInterface
41+
* @throws NoSuchEntityException
42+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
43+
*/
44+
public function afterGet(ProductRepositoryInterface $subject, ProductInterface $result, $sku, $editMode = false, $storeId = null, $forceReload = false): ProductInterface
45+
{
46+
if (ProductStatus::STATUS_ENABLED !== (int) $result->getStatus()) {
47+
throw new NoSuchEntityException(
48+
__("The product that was requested doesn't exist. Verify the product and try again.")
49+
);
50+
}
51+
52+
$context = $this->contextFactory->get();
53+
$store = $context->getExtensionAttributes()->getStore();
54+
if (!in_array($store->getWebsiteId(), $result->getWebsiteIds())) {
55+
throw new NoSuchEntityException(
56+
__("The product that was requested doesn't exist. Verify the product and try again.")
57+
);
58+
}
59+
60+
return $result;
61+
}
62+
}

app/code/Magento/CatalogGraphQl/etc/graphql/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,7 @@
188188
</argument>
189189
</arguments>
190190
</type>
191+
<type name="Magento\Catalog\Api\ProductRepositoryInterface">
192+
<plugin name="availableProductsFilter" type="Magento\CatalogGraphQl\Plugin\AvailableProductsFilter" />
193+
</type>
191194
</config>

dev/tests/integration/testsuite/Magento/Review/_files/set_position_and_add_store_to_all_ratings.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66

77
declare(strict_types=1);
88

9-
use Magento\Backend\App\Area\FrontNameResolver;
109
use Magento\Review\Model\ResourceModel\Rating\Collection as RatingCollection;
1110
use Magento\Review\Model\ResourceModel\Rating as RatingResourceModel;
1211
use Magento\Store\Model\StoreManagerInterface;
1312
use Magento\TestFramework\Helper\Bootstrap;
1413

15-
Bootstrap::getInstance()->loadArea(FrontNameResolver::AREA_CODE);
16-
1714
$objectManager = Bootstrap::getObjectManager();
1815

1916
$storeId = $objectManager->get(StoreManagerInterface::class)->getStore()->getId();

dev/tests/integration/testsuite/Magento/Review/_files/set_position_and_add_store_to_all_ratings_rollback.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66

77
declare(strict_types=1);
88

9-
use Magento\Backend\App\Area\FrontNameResolver;
109
use Magento\Review\Model\ResourceModel\Rating\Collection as RatingCollection;
1110
use Magento\Review\Model\ResourceModel\Rating as RatingResourceModel;
1211
use Magento\Store\Model\StoreManagerInterface;
1312
use Magento\TestFramework\Helper\Bootstrap;
1413

15-
Bootstrap::getInstance()->loadArea(FrontNameResolver::AREA_CODE);
1614
$objectManager = Bootstrap::getObjectManager();
1715

1816
$storeId = Bootstrap::getObjectManager()->get(StoreManagerInterface::class)->getStore()->getId();

0 commit comments

Comments
 (0)