ConfiguredOptions.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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\Catalog\Pricing\Price;
  8. use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
  9. /**
  10. * Configured Options model.
  11. */
  12. class ConfiguredOptions
  13. {
  14. /**
  15. * Get value of configured options.
  16. *
  17. * @param float $basePrice
  18. * @param ItemInterface $item
  19. *
  20. * @return float
  21. */
  22. public function getItemOptionsValue(float $basePrice, ItemInterface $item): float
  23. {
  24. $product = $item->getProduct();
  25. $value = 0.;
  26. $optionIds = $item->getOptionByCode('option_ids');
  27. if ($optionIds) {
  28. foreach (explode(',', $optionIds->getValue()) as $optionId) {
  29. $option = $product->getOptionById($optionId);
  30. if ($option !== null) {
  31. $itemOption = $item->getOptionByCode('option_' . $option->getId());
  32. /** @var $group \Magento\Catalog\Model\Product\Option\Type\DefaultType */
  33. $group = $option->groupFactory($option->getType())
  34. ->setOption($option)
  35. ->setConfigurationItem($item)
  36. ->setConfigurationItemOption($itemOption);
  37. $value += $group->getOptionPrice($itemOption->getValue(), $basePrice);
  38. }
  39. }
  40. }
  41. return $value;
  42. }
  43. }