Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

magento/magento2#29926: Prices should be possibly hidden from products query results #29998

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we could make the Money! type not mandatory, would be better.
We won't now though, because it's backward incompatible in schema. I guess we never thought a price can be optional...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, so this seemed like the good approach under these circumstances.

'regular_price' => [
'value' => null,
'currency' => null
],
'final_price' => [
'value' => null,
'currency' => null
],
'discount' => null
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's no constant in core for this right? if not..leave it like this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, no constant as far I can see.

)
&& $specialPrice->getValue()) {
return $specialPrice->getValue();
}

Expand Down