FinalPrice.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GroupedProduct\Pricing\Price;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Catalog\Pricing\Price\FinalPriceInterface;
  9. /**
  10. * Final price model
  11. */
  12. class FinalPrice extends \Magento\Catalog\Pricing\Price\FinalPrice implements FinalPriceInterface
  13. {
  14. /**
  15. * Price type final
  16. */
  17. const PRICE_CODE = 'final_price';
  18. /**
  19. * @var Product
  20. */
  21. protected $minProduct;
  22. /**
  23. * Return minimal product price
  24. *
  25. * @return float
  26. */
  27. public function getValue()
  28. {
  29. $minProduct = $this->getMinProduct();
  30. return $minProduct ?
  31. $minProduct->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE)->getValue() :
  32. 0.00;
  33. }
  34. /**
  35. * Returns product with minimal price
  36. *
  37. * @return Product
  38. */
  39. public function getMinProduct()
  40. {
  41. if (null === $this->minProduct) {
  42. $products = $this->product->getTypeInstance()->getAssociatedProducts($this->product);
  43. $minPrice = null;
  44. foreach ($products as $item) {
  45. $product = clone $item;
  46. $product->setQty(\Magento\Framework\Pricing\PriceInfoInterface::PRODUCT_QUANTITY_DEFAULT);
  47. $price = $product->getPriceInfo()
  48. ->getPrice(FinalPrice::PRICE_CODE)
  49. ->getValue();
  50. if (($price !== false) && ($price <= ($minPrice === null ? $price : $minPrice))) {
  51. $this->minProduct = $product;
  52. $minPrice = $price;
  53. }
  54. }
  55. }
  56. return $this->minProduct;
  57. }
  58. }