MinimalTierPriceCalculator.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Pricing\Price;
  7. use Magento\Framework\Pricing\SaleableInterface;
  8. use Magento\Framework\Pricing\Adjustment\CalculatorInterface;
  9. use Magento\Framework\Pricing\Amount\AmountInterface;
  10. /**
  11. * As Low As shows minimal value of Tier Prices
  12. */
  13. class MinimalTierPriceCalculator implements MinimalPriceCalculatorInterface
  14. {
  15. /**
  16. * @var CalculatorInterface
  17. */
  18. private $calculator;
  19. /**
  20. * @param CalculatorInterface $calculator
  21. */
  22. public function __construct(CalculatorInterface $calculator)
  23. {
  24. $this->calculator = $calculator;
  25. }
  26. /**
  27. * Get raw value of "as low as" as a minimal among tier prices
  28. * {@inheritdoc}
  29. */
  30. public function getValue(SaleableInterface $saleableItem)
  31. {
  32. /** @var TierPrice $price */
  33. $price = $saleableItem->getPriceInfo()->getPrice(TierPrice::PRICE_CODE);
  34. $tierPriceList = $price->getTierPriceList();
  35. $tierPrices = [];
  36. foreach ($tierPriceList as $tierPrice) {
  37. /** @var AmountInterface $price */
  38. $price = $tierPrice['price'];
  39. $tierPrices[] = $price->getValue();
  40. }
  41. return $tierPrices ? min($tierPrices) : null;
  42. }
  43. /**
  44. * Return calculated amount object that keeps "as low as" value
  45. * {@inheritdoc}
  46. */
  47. public function getAmount(SaleableInterface $saleableItem)
  48. {
  49. $value = $this->getValue($saleableItem);
  50. return $value === null
  51. ? null
  52. : $this->calculator->getAmount($value, $saleableItem);
  53. }
  54. }