Skip to content

Commit 8133fa1

Browse files
committed
Merge remote-tracking branch 'magento-l3/ACP2E-175' into L3_PR_21-10-19
2 parents 9b5d424 + 082d20d commit 8133fa1

File tree

13 files changed

+130
-26
lines changed

13 files changed

+130
-26
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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(
45+
ProductRepositoryInterface $subject,
46+
ProductInterface $result,
47+
$sku,
48+
$editMode = false,
49+
$storeId = null,
50+
$forceReload = false
51+
): ProductInterface {
52+
if (ProductStatus::STATUS_ENABLED !== (int) $result->getStatus()) {
53+
throw new NoSuchEntityException(
54+
__("The product that was requested doesn't exist. Verify the product and try again.")
55+
);
56+
}
57+
58+
$context = $this->contextFactory->get();
59+
$store = $context->getExtensionAttributes()->getStore();
60+
if (!in_array($store->getWebsiteId(), $result->getWebsiteIds())) {
61+
throw new NoSuchEntityException(
62+
__("The product that was requested doesn't exist. Verify the product and try again.")
63+
);
64+
}
65+
66+
return $result;
67+
}
68+
}

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>

app/code/Magento/ConfigurableProductGraphQl/Model/Cart/BuyRequest/SuperAttributeDataProvider.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ public function execute(array $cartItemData): array
9292
} catch (NoSuchEntityException $e) {
9393
throw new GraphQlNoSuchEntityException(__('Could not find specified product.'));
9494
}
95-
if (!in_array($cart->getStore()->getWebsiteId(), $product->getWebsiteIds())) {
96-
throw new GraphQlNoSuchEntityException(__('Could not find specified product.'));
97-
}
9895

9996
$this->checkProductStock($sku, (float) $qty, (int) $cart->getStore()->getWebsiteId());
10097

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,6 @@ private function addItemToCart(CartInterface $cart, Data\CartItem $cartItem, int
151151
return;
152152
}
153153

154-
if (!in_array($cart->getStoreId(), $product->getStoreIds())) {
155-
$this->addError(
156-
__('Could not find a product with SKU "%sku"', ['sku' => $sku])->render(),
157-
$cartItemPosition
158-
);
159-
160-
return;
161-
}
162-
163154
try {
164155
$result = $cart->addProduct($product, $this->requestBuilder->build($cartItem));
165156
$this->cartRepository->save($cart);

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ public function execute(Quote $cart, array $cartItemData): void
6060
} catch (NoSuchEntityException $e) {
6161
throw new GraphQlNoSuchEntityException(__('Could not find a product with SKU "%sku"', ['sku' => $sku]));
6262
}
63-
if (!in_array($cart->getStore()->getWebsiteId(), $product->getWebsiteIds())) {
64-
throw new GraphQlNoSuchEntityException(__('Could not find a product with SKU "%sku"', ['sku' => $sku]));
65-
}
6663

6764
try {
6865
$result = $cart->addProduct($product, $this->buyRequestBuilder->build($cartItemData));

app/code/Magento/Wishlist/Model/Wishlist/AddProductsToWishlist.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,6 @@ private function addItemToWishlist(Wishlist $wishlist, WishlistItem $wishlistIte
117117
if ((int)$wishlistItem->getQuantity() === 0) {
118118
throw new LocalizedException(__("The quantity of a wish list item cannot be 0"));
119119
}
120-
if ($product->getStatus() == Status::STATUS_DISABLED) {
121-
throw new LocalizedException(__("The product is disabled"));
122-
}
123120
$options = $this->buyRequestBuilder->build($wishlistItem, (int) $product->getId());
124121
$result = $wishlist->addNewItem($product, $options);
125122

dev/tests/api-functional/testsuite/Magento/GraphQl/ConfigurableProduct/AddConfigurableProductToCartTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,25 @@ public function testAddMoreProductsFromAnotherStore()
593593
self::assertEquals($quantity * 2, $cartItem['quantity']);
594594
}
595595

596+
/**
597+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_in_multiple_websites_disable_first_child.php
598+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
599+
*/
600+
public function testAddConfigurableProductWithDisabledChildToCart(): void
601+
{
602+
$quantity = 1;
603+
$parentSku = 'configurable';
604+
$sku = 'simple_Option_1';
605+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
606+
607+
$this->expectException(\Exception::class);
608+
$this->expectExceptionMessage('Could not find specified product.');
609+
610+
$query = $this->getQuery($maskedQuoteId, $parentSku, $sku, $quantity);
611+
$headerMap = ['Store' => 'default'];
612+
$this->graphQlMutation($query, [], '', $headerMap);
613+
}
614+
596615
/**
597616
* @param string $maskedQuoteId
598617
* @param string $parentSku

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/AddSimpleProductToCartTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ public function testAddDisabledProductToCart(): void
196196

197197
$this->expectException(ResponseContainsErrorsException::class);
198198
$this->expectExceptionMessage(
199-
'Could not add the product with SKU ' . $sku . ' to the shopping cart: ' .
200-
'Product that you are trying to add is not available.'
199+
'GraphQL response contains errors: Could not find a product with SKU "' . $sku . '"'
201200
);
202201

203202
$this->graphQlMutation($query);

dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/CustomerWishlistTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public function testAddProductToWishlistWithDisabledProduct()
261261
$addToWishlistResponse['addProductsToWishlist']['wishlist']['items_count'],
262262
'Count is greater than 0'
263263
);
264-
$message = 'The product is disabled';
264+
$message = 'Could not find a product with SKU "' . $sku . '"';
265265
$this->assertEquals(
266266
$message,
267267
$addToWishlistResponse['addProductsToWishlist']['user_errors'][0]['message']
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
use Magento\Catalog\Api\Data\ProductAttributeInterface;
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\Catalog\Model\Product\Action;
11+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
14+
15+
Resolver::getInstance()->requireDataFixture('Magento/ConfigurableProduct/_files/configurable_product_two_websites.php');
16+
17+
$childSku = 'simple_Option_1';
18+
$objectManager = Bootstrap::getObjectManager();
19+
/** @var ProductRepositoryInterface $productRepository */
20+
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
21+
$childProduct = $productRepository->get($childSku);
22+
$productAction = Bootstrap::getObjectManager()->get(Action::class);
23+
$productAction->updateAttributes(
24+
[$childProduct->getEntityId()],
25+
[ProductAttributeInterface::CODE_STATUS => Status::STATUS_DISABLED],
26+
$childProduct->getStoreId()
27+
);

0 commit comments

Comments
 (0)