Skip to content

Commit

Permalink
Fix PR
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrsKondratjevs committed Apr 19, 2021
1 parent 8186870 commit ff855c2
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions Model/Resolver/PriceTiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,47 +30,42 @@ class PriceTiers extends SourcePriceTiers
/**
* @var TiersFactory
*/
private $tiersFactory;
protected $tiersFactory;

/**
* @var ValueFactory
*/
private $valueFactory;
protected $valueFactory;

/**
* @var GetCustomerGroup
*/
private $getCustomerGroup;
protected $getCustomerGroup;

/**
* @var int
*/
private $customerGroupId;
protected $customerGroupId;

/**
* @var Tiers
*/
private $tiers;
protected $tiers;

/**
* @var Discount
*/
private $discount;
protected $discount;

/**
* @var PriceCurrencyInterface
*/
private $priceCurrency;
protected $priceCurrency;

/**
* @var array
*/
private $formatAndFilterTierPrices = [];

/**
* @var array
*/
private $tierPricesQty = [];
protected $tierPricesQty = [];

/**
* @param ValueFactory $valueFactory
Expand Down Expand Up @@ -138,7 +133,6 @@ public function resolve(
return $this->valueFactory->create(
function () use ($productId, $context) {
// This lines added to clean previous results
$this->formatAndFilterTierPrices = [];
$this->tierPricesQty = [];

$currencyCode = $context->getExtensionAttributes()->getStore()->getCurrentCurrencyCode();
Expand All @@ -164,12 +158,16 @@ protected function formatAndFilterTierPrices(
array $tierPrices,
string $currencyCode
): array {
$result = [];

foreach ($tierPrices as $key => $tierPrice) {
$tierPrice->setValue($this->priceCurrency->convertAndRound($tierPrice->getValue()));
$this->formatTierPrices($productPrice, $currencyCode, $tierPrice);
$this->filterTierPrices($tierPrices, $key, $tierPrice);
$formattedTierPrices = $this->formatTierPrices($productPrice, $currencyCode, $tierPrice);
$filteredTierPrices = $this->filterTierPrices($tierPrices, $key, $tierPrice, $formattedTierPrices);
$result[] = $filteredTierPrices;
}
return $this->formatAndFilterTierPrices;

return $result;
}

/**
Expand All @@ -188,7 +186,7 @@ protected function formatTierPrices(float $productPrice, string $currencyCode, $
$discount = $this->discount->getDiscountByDifference($productPrice, (float)$tierPrice->getValue());
}

$this->formatAndFilterTierPrices[] = [
return [
"discount" => $discount,
"quantity" => $tierPrice->getQty(),
"final_price" => [
Expand All @@ -208,19 +206,22 @@ protected function formatTierPrices(float $productPrice, string $currencyCode, $
protected function filterTierPrices(
array $tierPrices,
int $key,
ProductTierPriceInterface $tierPriceItem
ProductTierPriceInterface $tierPriceItem,
$formattedTierPrice
) {
$qty = $tierPriceItem->getQty();
if (isset($this->tierPricesQty[$qty])) {
$priceQty = $this->tierPricesQty[$qty];
if ((float)$tierPriceItem->getValue() < (float)$tierPrices[$priceQty]->getValue()) {
unset($this->formatAndFilterTierPrices[$priceQty]);
unset($formattedTierPrice[$priceQty]);
$this->tierPricesQty[$priceQty] = $key;
} else {
unset($this->formatAndFilterTierPrices[$key]);
unset($formattedTierPrice[$key]);
}
} else {
$this->tierPricesQty[$qty] = $key;
}

return $formattedTierPrice;
}
}

0 comments on commit ff855c2

Please sign in to comment.