Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigurableRegularPrice.php
Go to the documentation of this file.
1 <?php
8 
12 
17 {
21  const PRICE_CODE = 'regular_price';
22 
26  protected $maxRegularAmount;
27 
31  protected $minRegularAmount;
32 
36  protected $values = [];
37 
41  protected $priceResolver;
42 
46  private $configurableOptionsProvider;
47 
51  private $lowestPriceOptionsProvider;
52 
61  public function __construct(
62  \Magento\Framework\Pricing\SaleableInterface $saleableItem,
63  $quantity,
64  \Magento\Framework\Pricing\Adjustment\CalculatorInterface $calculator,
67  LowestPriceOptionsProviderInterface $lowestPriceOptionsProvider = null
68  ) {
69  parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
70  $this->priceResolver = $priceResolver;
71  $this->lowestPriceOptionsProvider = $lowestPriceOptionsProvider ?:
72  ObjectManager::getInstance()->get(LowestPriceOptionsProviderInterface::class);
73  }
74 
78  public function getValue()
79  {
80  if (!isset($this->values[$this->product->getId()])) {
81  $this->values[$this->product->getId()] = $this->priceResolver->resolvePrice($this->product);
82  }
83 
84  return $this->values[$this->product->getId()];
85  }
86 
90  public function getAmount()
91  {
92  return $this->getMinRegularAmount();
93  }
94 
98  public function getMaxRegularAmount()
99  {
100  if (null === $this->maxRegularAmount) {
101  $this->maxRegularAmount = $this->doGetMaxRegularAmount() ?: false;
102  }
104  }
105 
111  protected function doGetMaxRegularAmount()
112  {
113  $maxAmount = null;
114  foreach ($this->getUsedProducts() as $product) {
115  $childPriceAmount = $product->getPriceInfo()->getPrice(self::PRICE_CODE)->getAmount();
116  if (!$maxAmount || ($childPriceAmount->getValue() > $maxAmount->getValue())) {
117  $maxAmount = $childPriceAmount;
118  }
119  }
120  return $maxAmount;
121  }
122 
126  public function getMinRegularAmount()
127  {
128  if (null === $this->minRegularAmount) {
129  $this->minRegularAmount = $this->doGetMinRegularAmount() ?: parent::getAmount();
130  }
132  }
133 
139  protected function doGetMinRegularAmount()
140  {
141  $minAmount = null;
142  foreach ($this->lowestPriceOptionsProvider->getProducts($this->product) as $product) {
143  $childPriceAmount = $product->getPriceInfo()->getPrice(self::PRICE_CODE)->getAmount();
144  if (!$minAmount || ($childPriceAmount->getValue() < $minAmount->getValue())) {
145  $minAmount = $childPriceAmount;
146  }
147  }
148  return $minAmount;
149  }
150 
156  protected function getUsedProducts()
157  {
158  return $this->getConfigurableOptionsProvider()->getProducts($this->product);
159  }
160 
165  private function getConfigurableOptionsProvider()
166  {
167  if (null === $this->configurableOptionsProvider) {
168  $this->configurableOptionsProvider = ObjectManager::getInstance()
169  ->get(ConfigurableOptionsProviderInterface::class);
170  }
171  return $this->configurableOptionsProvider;
172  }
173 }
__construct(\Magento\Framework\Pricing\SaleableInterface $saleableItem, $quantity, \Magento\Framework\Pricing\Adjustment\CalculatorInterface $calculator, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency, PriceResolverInterface $priceResolver, LowestPriceOptionsProviderInterface $lowestPriceOptionsProvider=null)