DiscountCalculator.php 1002 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Pricing\Price;
  7. use Magento\Catalog\Model\Product;
  8. /**
  9. * Class DiscountCalculator
  10. */
  11. class DiscountCalculator
  12. {
  13. /**
  14. * Apply percentage discount
  15. *
  16. * @param Product $product
  17. * @param float|null $value
  18. * @return float|null
  19. */
  20. public function calculateDiscount(Product $product, $value = null)
  21. {
  22. if ($value === null) {
  23. $value = $product->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE)->getValue();
  24. }
  25. $discount = null;
  26. foreach ($product->getPriceInfo()->getPrices() as $price) {
  27. if ($price instanceof DiscountProviderInterface && $price->getDiscountPercent()) {
  28. $discount = min($price->getDiscountPercent(), $discount ?: $price->getDiscountPercent());
  29. }
  30. }
  31. return (null !== $discount) ? $discount/100 * $value : $value;
  32. }
  33. }