Skip to content

Commit

Permalink
#1760 Correcct price in filetr with non-base currency (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
riha112 authored Dec 22, 2020
1 parent 853798e commit d21ef8d
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
127 changes: 127 additions & 0 deletions src/Model/Layer/Filter/Price.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* ScandiPWA - Progressive Web App for Magento
*
* Copyright © Scandiweb, Inc. All rights reserved.
* See LICENSE for license details.
*
* @license OSL-3.0 (Open Software License ("OSL") v. 3.0)
* @package scandipwa/module-customer-graph-ql
* @link https://github.com/scandipwa/module-customer-graph-ql
*/
declare(strict_types=1);

namespace ScandiPWA\CatalogGraphQl\Model\Layer\Filter;

use Magento\Store\Model\StoreManagerInterface;
use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\LayerBuilderInterface;
use Magento\Framework\Api\Search\AggregationInterface;
use Magento\Framework\Api\Search\BucketInterface;
use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Formatter\LayerFormatter;

/**
* @inheritdoc
*/
class Price implements LayerBuilderInterface
{
/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @var string
*/
private const PRICE_BUCKET = 'price_bucket';

/**
* @var LayerFormatter
*/
private $layerFormatter;

/**
* @var array
*/
private static $bucketMap = [
self::PRICE_BUCKET => [
'request_name' => 'price',
'label' => 'Price'
],
];

/**
* @param LayerFormatter $layerFormatter
*/
public function __construct(
LayerFormatter $layerFormatter,
StoreManagerInterface $storeManager
)
{
$this->layerFormatter = $layerFormatter;
$this->storeManager = $storeManager;
}

/**
* @inheritdoc
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function build(AggregationInterface $aggregation, ?int $storeId): array
{
$bucket = $aggregation->getBucket(self::PRICE_BUCKET);
if ($this->isBucketEmpty($bucket)) {
return [];
}

$result = $this->layerFormatter->buildLayer(
self::$bucketMap[self::PRICE_BUCKET]['label'],
\count($bucket->getValues()),
self::$bucketMap[self::PRICE_BUCKET]['request_name']
);

// Gets cuurrent currency rate
$currencyRate = $this->storeManager->getStore()->getCurrentCurrencyRate();

// Loops through-out each price range option
foreach ($bucket->getValues() as $value) {
$metrics = $value->getMetrics();

// Updates to correct currency
$priceRange = [
'from' => $this->getMetricValue($metrics['from'], $currencyRate),
'to' => $this->getMetricValue($metrics['to'], $currencyRate)
];

// Builds graph-ql response
$result['options'][] = $this->layerFormatter->buildItem(
$priceRange['from'] . '~' . $priceRange['to'],
$metrics['value'],
$metrics['count']
);
}

return [$result];
}

/**
* Converts price to correct currency base,
* if notehing is set, then changes it to wildcard.
*
* @param $base
* @param $rate
* @return float|int|string
*/
private function getMetricValue($base, $rate) {
return (!is_null($base) && is_numeric($base)) ? $base * $rate : '*';
}

/**
* Check that bucket contains data
*
* @param BucketInterface|null $bucket
* @return bool
*/
private function isBucketEmpty(?BucketInterface $bucket): bool
{
return null === $bucket || !$bucket->getValues();
}
}
3 changes: 3 additions & 0 deletions src/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,7 @@
<type name="Magento\Eav\Model\Entity\Collection\AbstractCollection">
<plugin name="find_duplicate_entry" type="ScandiPWA\CatalogGraphQl\Plugin\Collection" sortOrder="20"/>
</type>

<preference for="Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Price"
type="ScandiPWA\CatalogGraphQl\Model\Layer\Filter\Price" />
</config>

0 comments on commit d21ef8d

Please sign in to comment.