Skip to content

Commit 36790c4

Browse files
committed
#29926: Prices should be possibly hidden from products query results
Initial draft
1 parent 40f9a4b commit 36790c4

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ public function resolve(
110110
}
111111

112112
$product = $value['model'];
113+
114+
if ($product->hasData('can_show_price') && $product->getData('can_show_price') === false) {
115+
return [];
116+
}
117+
113118
$productId = $product->getId();
114119
$this->tiers->addProductFilter($productId);
115120

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/PriceRange.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\CatalogGraphQl\Model\Resolver\Product;
99

10+
use Magento\Catalog\Api\Data\ProductInterface;
1011
use Magento\CatalogGraphQl\Model\Resolver\Product\Price\Discount;
1112
use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderPool as PriceProviderPool;
1213
use Magento\Framework\GraphQl\Query\ResolverInterface;
@@ -66,10 +67,12 @@ public function resolve(
6667
$returnArray = [];
6768

6869
if (isset($requestedFields['minimum_price'])) {
69-
$returnArray['minimum_price'] = $this->getMinimumProductPrice($product, $store);
70+
$returnArray['minimum_price'] = $this->canShowPrice($product) ?
71+
$this->getMinimumProductPrice($product, $store) : $this->formatEmptyResult();
7072
}
7173
if (isset($requestedFields['maximum_price'])) {
72-
$returnArray['maximum_price'] = $this->getMaximumProductPrice($product, $store);
74+
$returnArray['maximum_price'] = $this->canShowPrice($product) ?
75+
$this->getMaximumProductPrice($product, $store) : $this->formatEmptyResult();
7376
}
7477
return $returnArray;
7578
}
@@ -130,4 +133,39 @@ private function formatPrice(float $regularPrice, float $finalPrice, StoreInterf
130133
'discount' => $this->discount->getDiscountByDifference($regularPrice, $finalPrice),
131134
];
132135
}
136+
137+
/**
138+
* Check if the product is allowed to show price
139+
*
140+
* @param ProductInterface $product
141+
* @return bool
142+
*/
143+
private function canShowPrice($product): bool
144+
{
145+
if ($product->hasData('can_show_price') && $product->getData('can_show_price') === false) {
146+
return false;
147+
}
148+
149+
return true;
150+
}
151+
152+
/**
153+
* Format empty result
154+
*
155+
* @return array
156+
*/
157+
private function formatEmptyResult(): array
158+
{
159+
return [
160+
'regular_price' => [
161+
'value' => null,
162+
'currency' => null
163+
],
164+
'final_price' => [
165+
'value' => null,
166+
'currency' => null
167+
],
168+
'discount' => null
169+
];
170+
}
133171
}

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
2828
/** @var PricingSpecialPrice $specialPrice */
2929
$specialPrice = $product->getPriceInfo()->getPrice(PricingSpecialPrice::PRICE_CODE);
3030

31-
if ($specialPrice->getValue()) {
31+
if ((!$product->hasData('can_show_price')
32+
|| ($product->hasData('can_show_price') && $product->getData('can_show_price') === true)
33+
)
34+
&& $specialPrice->getValue()) {
3235
return $specialPrice->getValue();
3336
}
3437

0 commit comments

Comments
 (0)