FinalPriceBox.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ConfigurableProduct\Pricing\Render;
  7. use Magento\Catalog\Model\Product\Pricing\Renderer\SalableResolverInterface;
  8. use Magento\Catalog\Pricing\Price\FinalPrice;
  9. use Magento\Catalog\Pricing\Price\MinimalPriceCalculatorInterface;
  10. use Magento\Catalog\Pricing\Price\RegularPrice;
  11. use Magento\ConfigurableProduct\Pricing\Price\ConfigurableOptionsProviderInterface;
  12. use Magento\ConfigurableProduct\Pricing\Price\LowestPriceOptionsProviderInterface;
  13. use Magento\Framework\App\ObjectManager;
  14. use Magento\Framework\Pricing\Price\PriceInterface;
  15. use Magento\Framework\Pricing\Render\RendererPool;
  16. use Magento\Framework\Pricing\SaleableInterface;
  17. use Magento\Framework\View\Element\Template\Context;
  18. class FinalPriceBox extends \Magento\Catalog\Pricing\Render\FinalPriceBox
  19. {
  20. /**
  21. * @var LowestPriceOptionsProviderInterface
  22. */
  23. private $lowestPriceOptionsProvider;
  24. /**
  25. * @param Context $context
  26. * @param SaleableInterface $saleableItem
  27. * @param PriceInterface $price
  28. * @param RendererPool $rendererPool
  29. * @param ConfigurableOptionsProviderInterface $configurableOptionsProvider
  30. * @param array $data
  31. * @param LowestPriceOptionsProviderInterface $lowestPriceOptionsProvider
  32. * @param SalableResolverInterface|null $salableResolver
  33. * @param MinimalPriceCalculatorInterface|null $minimalPriceCalculator
  34. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  35. */
  36. public function __construct(
  37. Context $context,
  38. SaleableInterface $saleableItem,
  39. PriceInterface $price,
  40. RendererPool $rendererPool,
  41. ConfigurableOptionsProviderInterface $configurableOptionsProvider,
  42. array $data = [],
  43. LowestPriceOptionsProviderInterface $lowestPriceOptionsProvider = null,
  44. SalableResolverInterface $salableResolver = null,
  45. MinimalPriceCalculatorInterface $minimalPriceCalculator = null
  46. ) {
  47. parent::__construct(
  48. $context,
  49. $saleableItem,
  50. $price,
  51. $rendererPool,
  52. $data,
  53. $salableResolver,
  54. $minimalPriceCalculator
  55. );
  56. $this->lowestPriceOptionsProvider = $lowestPriceOptionsProvider ?:
  57. ObjectManager::getInstance()->get(LowestPriceOptionsProviderInterface::class);
  58. }
  59. /**
  60. * Define if the special price should be shown
  61. *
  62. * @return bool
  63. */
  64. public function hasSpecialPrice()
  65. {
  66. $product = $this->getSaleableItem();
  67. foreach ($this->lowestPriceOptionsProvider->getProducts($product) as $subProduct) {
  68. $regularPrice = $subProduct->getPriceInfo()->getPrice(RegularPrice::PRICE_CODE)->getValue();
  69. $finalPrice = $subProduct->getPriceInfo()->getPrice(FinalPrice::PRICE_CODE)->getValue();
  70. if ($finalPrice < $regularPrice) {
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. }