123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\ConfigurableProduct\Pricing\Price;
- use Magento\Catalog\Model\Product;
- use Magento\Framework\App\ObjectManager;
- use Magento\Framework\Pricing\Price\AbstractPrice;
- /**
- * Class RegularPrice
- */
- class ConfigurableRegularPrice extends AbstractPrice implements ConfigurableRegularPriceInterface
- {
- /**
- * Price type
- */
- const PRICE_CODE = 'regular_price';
- /**
- * @var \Magento\Framework\Pricing\Amount\AmountInterface
- */
- protected $maxRegularAmount;
- /**
- * @var \Magento\Framework\Pricing\Amount\AmountInterface
- */
- protected $minRegularAmount;
- /**
- * @var array
- */
- protected $values = [];
- /**
- * @var \Magento\ConfigurableProduct\Pricing\Price\PriceResolverInterface
- */
- protected $priceResolver;
- /**
- * @var ConfigurableOptionsProviderInterface
- */
- private $configurableOptionsProvider;
- /**
- * @var LowestPriceOptionsProviderInterface
- */
- private $lowestPriceOptionsProvider;
- /**
- * @param \Magento\Framework\Pricing\SaleableInterface $saleableItem
- * @param float $quantity
- * @param \Magento\Framework\Pricing\Adjustment\CalculatorInterface $calculator
- * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
- * @param PriceResolverInterface $priceResolver
- * @param LowestPriceOptionsProviderInterface $lowestPriceOptionsProvider
- */
- public function __construct(
- \Magento\Framework\Pricing\SaleableInterface $saleableItem,
- $quantity,
- \Magento\Framework\Pricing\Adjustment\CalculatorInterface $calculator,
- \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
- PriceResolverInterface $priceResolver,
- LowestPriceOptionsProviderInterface $lowestPriceOptionsProvider = null
- ) {
- parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
- $this->priceResolver = $priceResolver;
- $this->lowestPriceOptionsProvider = $lowestPriceOptionsProvider ?:
- ObjectManager::getInstance()->get(LowestPriceOptionsProviderInterface::class);
- }
- /**
- * {@inheritdoc}
- */
- public function getValue()
- {
- if (!isset($this->values[$this->product->getId()])) {
- $this->values[$this->product->getId()] = $this->priceResolver->resolvePrice($this->product);
- }
- return $this->values[$this->product->getId()];
- }
- /**
- * {@inheritdoc}
- */
- public function getAmount()
- {
- return $this->getMinRegularAmount();
- }
- /**
- * {@inheritdoc}
- */
- public function getMaxRegularAmount()
- {
- if (null === $this->maxRegularAmount) {
- $this->maxRegularAmount = $this->doGetMaxRegularAmount() ?: false;
- }
- return $this->maxRegularAmount;
- }
- /**
- * Get max regular amount
- *
- * @return \Magento\Framework\Pricing\Amount\AmountInterface
- */
- protected function doGetMaxRegularAmount()
- {
- $maxAmount = null;
- foreach ($this->getUsedProducts() as $product) {
- $childPriceAmount = $product->getPriceInfo()->getPrice(self::PRICE_CODE)->getAmount();
- if (!$maxAmount || ($childPriceAmount->getValue() > $maxAmount->getValue())) {
- $maxAmount = $childPriceAmount;
- }
- }
- return $maxAmount;
- }
- /**
- * {@inheritdoc}
- */
- public function getMinRegularAmount()
- {
- if (null === $this->minRegularAmount) {
- $this->minRegularAmount = $this->doGetMinRegularAmount() ?: parent::getAmount();
- }
- return $this->minRegularAmount;
- }
- /**
- * Get min regular amount
- *
- * @return \Magento\Framework\Pricing\Amount\AmountInterface
- */
- protected function doGetMinRegularAmount()
- {
- $minAmount = null;
- foreach ($this->lowestPriceOptionsProvider->getProducts($this->product) as $product) {
- $childPriceAmount = $product->getPriceInfo()->getPrice(self::PRICE_CODE)->getAmount();
- if (!$minAmount || ($childPriceAmount->getValue() < $minAmount->getValue())) {
- $minAmount = $childPriceAmount;
- }
- }
- return $minAmount;
- }
- /**
- * Get children simple products
- *
- * @return Product[]
- */
- protected function getUsedProducts()
- {
- return $this->getConfigurableOptionsProvider()->getProducts($this->product);
- }
- /**
- * @return \Magento\ConfigurableProduct\Pricing\Price\ConfigurableOptionsProviderInterface
- * @deprecated 100.1.1
- */
- private function getConfigurableOptionsProvider()
- {
- if (null === $this->configurableOptionsProvider) {
- $this->configurableOptionsProvider = ObjectManager::getInstance()
- ->get(ConfigurableOptionsProviderInterface::class);
- }
- return $this->configurableOptionsProvider;
- }
- }
|