ConfiguredRegularPrice.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\GroupedProduct\Pricing\Price;
  8. use Magento\Catalog\Model\Product;
  9. use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
  10. use Magento\Framework\Pricing\Price\AbstractPrice;
  11. use Magento\Catalog\Pricing\Price\ConfiguredPriceInterface;
  12. /**
  13. * Configured regular price model.
  14. */
  15. class ConfiguredRegularPrice extends AbstractPrice implements ConfiguredPriceInterface
  16. {
  17. /**
  18. * Price type configured
  19. */
  20. const PRICE_CODE = ConfiguredPriceInterface::CONFIGURED_REGULAR_PRICE_CODE;
  21. /**
  22. * @var null|ItemInterface
  23. */
  24. private $item;
  25. /**
  26. * @param ItemInterface $item
  27. * @return $this
  28. */
  29. public function setItem(ItemInterface $item): ConfiguredRegularPrice
  30. {
  31. $this->item = $item;
  32. return $this;
  33. }
  34. /**
  35. * Calculate configured price.
  36. *
  37. * @return float
  38. */
  39. protected function calculatePrice(): float
  40. {
  41. $value = 0.;
  42. /** @var \Magento\GroupedProduct\Model\Product\Type\Grouped $typeInstance */
  43. $typeInstance = $this->getProduct()->getTypeInstance();
  44. $associatedProducts = $typeInstance
  45. ->setStoreFilter($this->getProduct()->getStore(), $this->getProduct())
  46. ->getAssociatedProducts($this->getProduct());
  47. foreach ($associatedProducts as $product) {
  48. /** @var Product $product */
  49. /** @var \Magento\Wishlist\Model\Item\Option $customOption */
  50. $customOption = $this->getProduct()
  51. ->getCustomOption('associated_product_' . $product->getId());
  52. if (!$customOption) {
  53. continue;
  54. }
  55. $finalPrice = $product->getPriceInfo()
  56. ->getPrice(\Magento\Catalog\Pricing\Price\RegularPrice::PRICE_CODE)
  57. ->getValue();
  58. $value += $finalPrice * ($customOption->getValue() ?: 1);
  59. }
  60. return $value;
  61. }
  62. /**
  63. * Price value of product with configured options.
  64. *
  65. * @return bool|float
  66. */
  67. public function getValue()
  68. {
  69. if ($this->item) {
  70. return $this->calculatePrice();
  71. } else {
  72. if ($this->value === null) {
  73. $price = $this->product->getPrice();
  74. $priceInCurrentCurrency = $this->priceCurrency->convertAndRound($price);
  75. $this->value = $priceInCurrentCurrency ? (float)$priceInCurrentCurrency : false;
  76. }
  77. return $this->value;
  78. }
  79. }
  80. }