MsrpPriceCalculator.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\MsrpConfigurableProduct\Pricing;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. use Magento\Catalog\Model\Product;
  10. use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
  11. use Magento\Msrp\Pricing\MsrpPriceCalculatorInterface;
  12. /**
  13. * {@inheritdoc}. Provide information for a Configurable product.
  14. */
  15. class MsrpPriceCalculator implements MsrpPriceCalculatorInterface
  16. {
  17. /**
  18. * @inheritdoc
  19. */
  20. public function getMsrpPriceValue(ProductInterface $product): float
  21. {
  22. /** @var Product $product */
  23. if ($product->getTypeId() !== Configurable::TYPE_CODE) {
  24. return 0;
  25. }
  26. /** @var Configurable $configurableProduct */
  27. $configurableProduct = $product->getTypeInstance();
  28. $msrp = 0;
  29. $prices = [];
  30. foreach ($configurableProduct->getUsedProducts($product) as $item) {
  31. if ($item->getMsrp() !== null) {
  32. $prices[] = $item->getMsrp();
  33. }
  34. }
  35. if ($prices) {
  36. $msrp = (float)max($prices);
  37. }
  38. return $msrp;
  39. }
  40. }