-
Notifications
You must be signed in to change notification settings - Fork 9.4k
[GraphQl] 29168 TierPrices filtering #29675
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
Changes from 9 commits
017c44e
603bc9f
257ba74
4f978a4
2cfd8a0
2d9740d
6c285ad
d535303
f498653
b2021d5
30901be
6b4383b
58eaf18
9f9164f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,19 +7,19 @@ | |
|
|
||
| namespace Magento\CatalogCustomerGraphQl\Model\Resolver; | ||
|
|
||
| use Magento\Catalog\Api\Data\ProductTierPriceInterface; | ||
| use Magento\CatalogCustomerGraphQl\Model\Resolver\Customer\GetCustomerGroup; | ||
| use Magento\CatalogCustomerGraphQl\Model\Resolver\Product\Price\Tiers; | ||
| use Magento\CatalogCustomerGraphQl\Model\Resolver\Product\Price\TiersFactory; | ||
| use Magento\CatalogGraphQl\Model\Resolver\Product\Price\Discount; | ||
| use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderPool as PriceProviderPool; | ||
| use Magento\Framework\Exception\LocalizedException; | ||
| use Magento\Framework\GraphQl\Config\Element\Field; | ||
| use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; | ||
| use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
| use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
| use Magento\Framework\Exception\LocalizedException; | ||
| use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; | ||
| use Magento\Framework\Pricing\PriceCurrencyInterface; | ||
| use Magento\CatalogCustomerGraphQl\Model\Resolver\Product\Price\Tiers; | ||
| use Magento\CatalogCustomerGraphQl\Model\Resolver\Product\Price\TiersFactory; | ||
| use Magento\CatalogCustomerGraphQl\Model\Resolver\Customer\GetCustomerGroup; | ||
| use Magento\Store\Api\Data\StoreInterface; | ||
| use Magento\CatalogGraphQl\Model\Resolver\Product\Price\Discount; | ||
| use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderPool as PriceProviderPool; | ||
| use Magento\Catalog\Api\Data\ProductTierPriceInterface; | ||
|
|
||
| /** | ||
| * Resolver for price_tiers | ||
|
|
@@ -120,42 +120,89 @@ function () use ($productId, $context) { | |
|
|
||
| $productPrice = $this->tiers->getProductRegularPrice($productId) ?? 0.0; | ||
| $tierPrices = $this->tiers->getProductTierPrices($productId) ?? []; | ||
|
|
||
| return $this->formatProductTierPrices($tierPrices, $productPrice, $store); | ||
| return $this->formatAndFilterTierPrices($tierPrices, $productPrice, $store); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Format tier prices for output | ||
| * Format and filter tier prices for output | ||
| * | ||
| * @param ProductTierPriceInterface[] $tierPrices | ||
| * @param float $productPrice | ||
| * @param StoreInterface $store | ||
| * @return array | ||
| */ | ||
| private function formatProductTierPrices(array $tierPrices, float $productPrice, StoreInterface $store): array | ||
| { | ||
| private function formatAndFilterTierPrices( | ||
| array $tierPrices, | ||
| float $productPrice, | ||
| StoreInterface $store | ||
| ): array { | ||
| $tiers = []; | ||
| $qtyCache = []; | ||
|
|
||
| foreach ($tierPrices as $key => $tierPrice) { | ||
| $this->formatTierPrices($productPrice, $store, $tierPrice, $tiers); | ||
| $this->filterTierPrices($tierPrices, $key, $tierPrice, $qtyCache, $tiers); | ||
pmarjan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| return $tiers; | ||
| } | ||
|
|
||
| /** | ||
| * Format tier prices for output | ||
| * | ||
| * @param float $productPrice | ||
| * @param StoreInterface $store | ||
| * @param ProductTierPriceInterface $tierPrice | ||
| * @param array $tiers | ||
| */ | ||
| private function formatTierPrices(float $productPrice, StoreInterface $store, &$tierPrice, &$tiers) | ||
|
||
| { | ||
| $tierPrice->setValue($this->priceCurrency->convertAndRound($tierPrice->getValue())); | ||
| $percentValue = $tierPrice->getExtensionAttributes()->getPercentageValue(); | ||
| if ($percentValue && is_numeric($percentValue)) { | ||
| $discount = $this->discount->getDiscountByPercent($productPrice, (float)$percentValue); | ||
| } else { | ||
| $discount = $this->discount->getDiscountByDifference($productPrice, (float)$tierPrice->getValue()); | ||
| } | ||
|
|
||
| foreach ($tierPrices as $tierPrice) { | ||
| $tierPrice->setValue($this->priceCurrency->convertAndRound($tierPrice->getValue())); | ||
| $percentValue = $tierPrice->getExtensionAttributes()->getPercentageValue(); | ||
| if ($percentValue && is_numeric($percentValue)) { | ||
| $discount = $this->discount->getDiscountByPercent($productPrice, (float)$percentValue); | ||
| $tiers[] = [ | ||
| "discount" => $discount, | ||
| "quantity" => $tierPrice->getQty(), | ||
| "final_price" => [ | ||
| "value" => $tierPrice->getValue(), | ||
| "currency" => $store->getCurrentCurrencyCode() | ||
| ] | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Filter the lowest price for each quantity | ||
| * | ||
| * @param array $tierPrices | ||
| * @param int $key | ||
| * @param ProductTierPriceInterface $tierPriceItem | ||
| * @param array $qtyCache | ||
| * @param array $tiers | ||
| */ | ||
| private function filterTierPrices( | ||
| array $tierPrices, | ||
| int $key, | ||
| ProductTierPriceInterface $tierPriceItem, | ||
| array &$qtyCache, | ||
|
||
| array &$tiers | ||
| ) { | ||
| $qty = $tierPriceItem->getQty(); | ||
| if (isset($qtyCache[$qty])) { | ||
| $priceQty = $qtyCache[$qty]; | ||
| if ((float)$tierPriceItem->getValue() < (float)$tierPrices[$priceQty]->getValue()) { | ||
| unset($tiers[$priceQty]); | ||
| $qtyCache[$priceQty] = $key; | ||
| } else { | ||
| $discount = $this->discount->getDiscountByDifference($productPrice, (float)$tierPrice->getValue()); | ||
| unset($tiers[$key]); | ||
| } | ||
|
|
||
| $tiers[] = [ | ||
| "discount" => $discount, | ||
| "quantity" => $tierPrice->getQty(), | ||
| "final_price" => [ | ||
| "value" => $tierPrice->getValue(), | ||
| "currency" => $store->getCurrentCurrencyCode() | ||
| ] | ||
| ]; | ||
| } else { | ||
| $qtyCache[$qty] = $key; | ||
| } | ||
| return $tiers; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you fix types on
$this->tiers->addProductFilter($productId)?\Magento\CatalogCustomerGraphQl\Model\Resolver\Product\Price\Tiers::addProductFilterhas no strict typespublic function addProductFilter($productId): voidwould be
public function addProductFilter(int $productId): voidThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done