From 36790c4635549a020e771db0857772f893fb5f89 Mon Sep 17 00:00:00 2001 From: Marjan Date: Fri, 11 Sep 2020 12:19:06 +0200 Subject: [PATCH] magento/magento2#29926: Prices should be possibly hidden from products query results Initial draft --- .../Model/Resolver/PriceTiers.php | 5 +++ .../Model/Resolver/Product/PriceRange.php | 42 ++++++++++++++++++- .../Model/Resolver/Product/SpecialPrice.php | 5 ++- 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php b/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php index e78224ba0af38..9bc0d87061a01 100644 --- a/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php +++ b/app/code/Magento/CatalogCustomerGraphQl/Model/Resolver/PriceTiers.php @@ -110,6 +110,11 @@ public function resolve( } $product = $value['model']; + + if ($product->hasData('can_show_price') && $product->getData('can_show_price') === false) { + return []; + } + $productId = $product->getId(); $this->tiers->addProductFilter($productId); diff --git a/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/PriceRange.php b/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/PriceRange.php index dbb52f2010930..805571d58d634 100644 --- a/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/PriceRange.php +++ b/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/PriceRange.php @@ -7,6 +7,7 @@ namespace Magento\CatalogGraphQl\Model\Resolver\Product; +use Magento\Catalog\Api\Data\ProductInterface; use Magento\CatalogGraphQl\Model\Resolver\Product\Price\Discount; use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderPool as PriceProviderPool; use Magento\Framework\GraphQl\Query\ResolverInterface; @@ -66,10 +67,12 @@ public function resolve( $returnArray = []; if (isset($requestedFields['minimum_price'])) { - $returnArray['minimum_price'] = $this->getMinimumProductPrice($product, $store); + $returnArray['minimum_price'] = $this->canShowPrice($product) ? + $this->getMinimumProductPrice($product, $store) : $this->formatEmptyResult(); } if (isset($requestedFields['maximum_price'])) { - $returnArray['maximum_price'] = $this->getMaximumProductPrice($product, $store); + $returnArray['maximum_price'] = $this->canShowPrice($product) ? + $this->getMaximumProductPrice($product, $store) : $this->formatEmptyResult(); } return $returnArray; } @@ -130,4 +133,39 @@ private function formatPrice(float $regularPrice, float $finalPrice, StoreInterf 'discount' => $this->discount->getDiscountByDifference($regularPrice, $finalPrice), ]; } + + /** + * Check if the product is allowed to show price + * + * @param ProductInterface $product + * @return bool + */ + private function canShowPrice($product): bool + { + if ($product->hasData('can_show_price') && $product->getData('can_show_price') === false) { + return false; + } + + return true; + } + + /** + * Format empty result + * + * @return array + */ + private function formatEmptyResult(): array + { + return [ + 'regular_price' => [ + 'value' => null, + 'currency' => null + ], + 'final_price' => [ + 'value' => null, + 'currency' => null + ], + 'discount' => null + ]; + } } diff --git a/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php b/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php index 1b42b0fde2bcb..c80cc3744876f 100644 --- a/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php +++ b/app/code/Magento/CatalogGraphQl/Model/Resolver/Product/SpecialPrice.php @@ -28,7 +28,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value /** @var PricingSpecialPrice $specialPrice */ $specialPrice = $product->getPriceInfo()->getPrice(PricingSpecialPrice::PRICE_CODE); - if ($specialPrice->getValue()) { + if ((!$product->hasData('can_show_price') + || ($product->hasData('can_show_price') && $product->getData('can_show_price') === true) + ) + && $specialPrice->getValue()) { return $specialPrice->getValue(); }