ConfiguredPrice.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Pricing\Price;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
  9. use Magento\Framework\Pricing\Adjustment\CalculatorInterface;
  10. use Magento\Framework\App\ObjectManager;
  11. /**
  12. * Configured price model
  13. */
  14. class ConfiguredPrice extends FinalPrice implements ConfiguredPriceInterface
  15. {
  16. /**
  17. * Price type configured
  18. */
  19. const PRICE_CODE = self::CONFIGURED_PRICE_CODE;
  20. /**
  21. * @var null|ItemInterface
  22. */
  23. protected $item;
  24. /**
  25. * @var ConfiguredOptions
  26. */
  27. private $configuredOptions;
  28. /**
  29. * @param Product $saleableItem
  30. * @param float $quantity
  31. * @param CalculatorInterface $calculator
  32. * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  33. * @param ItemInterface|null $item
  34. * @param ConfiguredOptions|null $configuredOptions
  35. */
  36. public function __construct(
  37. Product $saleableItem,
  38. $quantity,
  39. CalculatorInterface $calculator,
  40. \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
  41. ItemInterface $item = null,
  42. ConfiguredOptions $configuredOptions = null
  43. ) {
  44. $this->item = $item;
  45. $this->configuredOptions = $configuredOptions ?: ObjectManager::getInstance()->get(ConfiguredOptions::class);
  46. parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
  47. }
  48. /**
  49. * @param ItemInterface $item
  50. * @return $this
  51. */
  52. public function setItem(ItemInterface $item)
  53. {
  54. $this->item = $item;
  55. return $this;
  56. }
  57. /**
  58. * Get value of configured options.
  59. *
  60. * @deprecated 102.0.4 ConfiguredOptions::getItemOptionsValue is used instead
  61. * @return float
  62. */
  63. protected function getOptionsValue(): float
  64. {
  65. $product = $this->item->getProduct();
  66. $value = 0.;
  67. $basePrice = parent::getValue();
  68. $optionIds = $this->item->getOptionByCode('option_ids');
  69. if ($optionIds) {
  70. foreach (explode(',', $optionIds->getValue()) as $optionId) {
  71. $option = $product->getOptionById($optionId);
  72. if ($option) {
  73. $itemOption = $this->item->getOptionByCode('option_' . $option->getId());
  74. /** @var $group \Magento\Catalog\Model\Product\Option\Type\DefaultType */
  75. $group = $option->groupFactory($option->getType())
  76. ->setOption($option)
  77. ->setConfigurationItem($this->item)
  78. ->setConfigurationItemOption($itemOption);
  79. $value += $group->getOptionPrice($itemOption->getValue(), $basePrice);
  80. }
  81. }
  82. }
  83. return $value;
  84. }
  85. /**
  86. * Price value of product with configured options.
  87. *
  88. * @return bool|float
  89. */
  90. public function getValue()
  91. {
  92. $basePrice = parent::getValue();
  93. return $this->item
  94. ? $basePrice + $this->configuredOptions->getItemOptionsValue($basePrice, $this->item)
  95. : $basePrice;
  96. }
  97. }