Skip to content

Commit 3221fa6

Browse files
committed
Merge branch 'ACP2E-1417' of https://github.com/magento-l3/magento2ce into PR01262023
2 parents 519809e + cf3e947 commit 3221fa6

File tree

2 files changed

+166
-17
lines changed

2 files changed

+166
-17
lines changed

app/code/Magento/ConfigurableProductGraphQl/Model/Resolver/Product/Price/Provider.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderInterface;
1414
use Magento\ConfigurableProduct\Pricing\Price\ConfigurableOptionsProviderInterface;
1515
use Magento\Framework\Pricing\Amount\AmountInterface;
16+
use Magento\Framework\Pricing\Amount\BaseFactory;
1617
use Magento\Framework\Pricing\SaleableInterface;
1718

1819
/**
@@ -25,6 +26,11 @@ class Provider implements ProviderInterface
2526
*/
2627
private $optionsProvider;
2728

29+
/**
30+
* @var BaseFactory
31+
*/
32+
private $amountFactory;
33+
2834
/**
2935
* @var array
3036
*/
@@ -43,11 +49,14 @@ class Provider implements ProviderInterface
4349

4450
/**
4551
* @param ConfigurableOptionsProviderInterface $optionsProvider
52+
* @param BaseFactory $amountFactory
4653
*/
4754
public function __construct(
48-
ConfigurableOptionsProviderInterface $optionsProvider
55+
ConfigurableOptionsProviderInterface $optionsProvider,
56+
BaseFactory $amountFactory
4957
) {
5058
$this->optionsProvider = $optionsProvider;
59+
$this->amountFactory = $amountFactory;
5160
}
5261

5362
/**
@@ -101,7 +110,7 @@ private function getMinimalPrice(SaleableInterface $product, string $code): Amou
101110
{
102111
if (!isset($this->minimalPrice[$code][$product->getId()])) {
103112
$minimumAmount = null;
104-
foreach ($this->filterDisabledProducts($this->optionsProvider->getProducts($product)) as $variant) {
113+
foreach ($this->optionsProvider->getProducts($product) as $variant) {
105114
$variantAmount = $variant->getPriceInfo()->getPrice($code)->getAmount();
106115
if (!$minimumAmount || ($variantAmount->getValue() < $minimumAmount->getValue())) {
107116
$minimumAmount = $variantAmount;
@@ -110,7 +119,7 @@ private function getMinimalPrice(SaleableInterface $product, string $code): Amou
110119
}
111120
}
112121

113-
return $this->minimalPrice[$code][$product->getId()];
122+
return $this->minimalPrice[$code][$product->getId()] ?? $this->amountFactory->create(['amount' => null]);
114123
}
115124

116125
/**
@@ -133,19 +142,6 @@ private function getMaximalPrice(SaleableInterface $product, string $code): Amou
133142
}
134143
}
135144

136-
return $this->maximalPrice[$code][$product->getId()];
137-
}
138-
139-
/**
140-
* Filter out disabled products
141-
*
142-
* @param array $products
143-
* @return array
144-
*/
145-
private function filterDisabledProducts(array $products): array
146-
{
147-
return array_filter($products, function ($product) {
148-
return (int)$product->getStatus() === ProductStatus::STATUS_ENABLED;
149-
});
145+
return $this->maximalPrice[$code][$product->getId()] ?? $this->amountFactory->create(['amount' => null]);
150146
}
151147
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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\GraphQl\Quote;
9+
10+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
11+
use Magento\ConfigurableProduct\Test\Fixture\AddProductToCart as AddConfigurableProductToCartFixture;
12+
use Magento\ConfigurableProduct\Test\Fixture\Attribute as AttributeFixture;
13+
use Magento\ConfigurableProduct\Test\Fixture\Product as ConfigurableProductFixture;
14+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
15+
use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture;
16+
use Magento\TestFramework\Fixture\DataFixture;
17+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
18+
use Magento\TestFramework\Fixture\DataFixtureStorage;
19+
use Magento\TestFramework\Helper\Bootstrap;
20+
use Magento\TestFramework\TestCase\GraphQlAbstract;
21+
22+
/**
23+
* Configurable product in cart testcases
24+
*/
25+
class QuoteConfigurableProductInCartTest extends GraphQlAbstract
26+
{
27+
/**
28+
* @var DataFixtureStorage
29+
*/
30+
private $fixtures;
31+
32+
/**
33+
* @var QuoteIdToMaskedQuoteIdInterface
34+
*/
35+
private $quoteIdToMaskedQuoteIdInterface;
36+
37+
/**
38+
* @var \Magento\Catalog\Model\ProductRepository
39+
*/
40+
private $productRepository;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp(): void
46+
{
47+
$objectManager = Bootstrap::getObjectManager();
48+
$this->fixtures = $objectManager->get(DataFixtureStorageManager::class)->getStorage();
49+
$this->quoteIdToMaskedQuoteIdInterface = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
50+
$this->productRepository = $objectManager->get(\Magento\Catalog\Model\ProductRepository::class);
51+
}
52+
53+
#[
54+
DataFixture(ProductFixture::class, as: 'p1'),
55+
DataFixture(ProductFixture::class, as: 'p2'),
56+
DataFixture(AttributeFixture::class, as: 'attr'),
57+
DataFixture(
58+
ConfigurableProductFixture::class,
59+
['_options' => ['$attr$'], '_links' => ['$p1$', '$p2$']],
60+
'cp1'
61+
),
62+
DataFixture(GuestCartFixture::class, as: 'cart'),
63+
DataFixture(
64+
AddConfigurableProductToCartFixture::class,
65+
['cart_id' => '$cart.id$', 'product_id' => '$cp1.id$', 'child_product_id' => '$p1.id$', 'qty' => 1],
66+
),
67+
]
68+
public function testConfigurableProductInCartAfterGoesOutOfStock()
69+
{
70+
$product1 = $this->fixtures->get('p1');
71+
$product1 = $this->productRepository->get($product1->getSku(), true);
72+
$stockItem = $product1->getExtensionAttributes()->getStockItem();
73+
$stockItem->setQty(0);
74+
$stockItem->setIsInStock(false);
75+
$this->productRepository->save($product1);
76+
77+
$product2 = $this->fixtures->get('p2');
78+
$product2 = $this->productRepository->get($product2->getSku(), true);
79+
$stockItem = $product2->getExtensionAttributes()->getStockItem();
80+
$stockItem->setQty(0);
81+
$stockItem->setIsInStock(false);
82+
$this->productRepository->save($product2);
83+
$cart = $this->fixtures->get('cart');
84+
$maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int)$cart->getId());
85+
86+
$query = <<<'QUERY'
87+
query GetCartDetails($cartId: String!) {
88+
cart(cart_id: $cartId) {
89+
id
90+
items {
91+
uid
92+
product {
93+
uid
94+
name
95+
sku
96+
stock_status
97+
price_range {
98+
minimum_price {
99+
final_price {
100+
currency
101+
value
102+
}
103+
regular_price {
104+
currency
105+
value
106+
}
107+
}
108+
maximum_price {
109+
final_price {
110+
currency
111+
value
112+
}
113+
regular_price {
114+
currency
115+
value
116+
}
117+
}
118+
}
119+
}
120+
prices {
121+
price {
122+
currency
123+
value
124+
}
125+
}
126+
}
127+
}
128+
}
129+
QUERY;
130+
131+
$variables = [
132+
'cartId' => $maskedQuoteId
133+
];
134+
135+
$response = $this->graphQlQuery($query, $variables);
136+
$this->assertEquals($maskedQuoteId, $response['cart']['id'], 'Assert that correct quote is queried');
137+
$this->assertEquals(
138+
'OUT_OF_STOCK',
139+
$response['cart']['items'][0]['product']['stock_status'],
140+
'Assert product is out of stock'
141+
);
142+
$this->assertEquals(
143+
0,
144+
$response['cart']['items'][0]['product']['price_range']['minimum_price']['final_price']['value'],
145+
'Assert that minimum price equals to 0'
146+
);
147+
$this->assertEquals(
148+
0,
149+
$response['cart']['items'][0]['product']['price_range']['maximum_price']['final_price']['value'],
150+
'Assert that maximum price equals to 0'
151+
);
152+
}
153+
}

0 commit comments

Comments
 (0)