ConfigurablePriceResolver.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Pricing\Price;
  7. use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Pricing\PriceCurrencyInterface;
  10. class ConfigurablePriceResolver implements PriceResolverInterface
  11. {
  12. /**
  13. * @var \Magento\ConfigurableProduct\Pricing\Price\PriceResolverInterface
  14. */
  15. protected $priceResolver;
  16. /**
  17. * @var PriceCurrencyInterface
  18. * @deprecated 100.0.2
  19. */
  20. protected $priceCurrency;
  21. /**
  22. * @var Configurable
  23. * @deprecated 100.0.2
  24. */
  25. protected $configurable;
  26. /**
  27. * @var LowestPriceOptionsProviderInterface
  28. */
  29. private $lowestPriceOptionsProvider;
  30. /**
  31. * @param PriceResolverInterface $priceResolver
  32. * @param Configurable $configurable
  33. * @param PriceCurrencyInterface $priceCurrency
  34. * @param LowestPriceOptionsProviderInterface $lowestPriceOptionsProvider
  35. */
  36. public function __construct(
  37. PriceResolverInterface $priceResolver,
  38. Configurable $configurable,
  39. PriceCurrencyInterface $priceCurrency,
  40. LowestPriceOptionsProviderInterface $lowestPriceOptionsProvider = null
  41. ) {
  42. $this->priceResolver = $priceResolver;
  43. $this->configurable = $configurable;
  44. $this->priceCurrency = $priceCurrency;
  45. $this->lowestPriceOptionsProvider = $lowestPriceOptionsProvider ?:
  46. ObjectManager::getInstance()->get(LowestPriceOptionsProviderInterface::class);
  47. }
  48. /**
  49. * @param \Magento\Framework\Pricing\SaleableInterface|\Magento\Catalog\Model\Product $product
  50. * @return float
  51. * @throws \Magento\Framework\Exception\LocalizedException
  52. */
  53. public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product)
  54. {
  55. $price = null;
  56. foreach ($this->lowestPriceOptionsProvider->getProducts($product) as $subProduct) {
  57. $productPrice = $this->priceResolver->resolvePrice($subProduct);
  58. $price = isset($price) ? min($price, $productPrice) : $productPrice;
  59. }
  60. return (float)$price;
  61. }
  62. }