Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfiguredPrice.php
Go to the documentation of this file.
1 <?php
8 
13 
18 {
23 
27  protected $item;
28 
32  private $configuredOptions;
33 
42  public function __construct(
43  Product $saleableItem,
44  $quantity,
47  ItemInterface $item = null,
48  ConfiguredOptions $configuredOptions = null
49  ) {
50  $this->item = $item;
51  $this->configuredOptions = $configuredOptions ?: ObjectManager::getInstance()->get(ConfiguredOptions::class);
52  parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
53  }
54 
59  public function setItem(ItemInterface $item)
60  {
61  $this->item = $item;
62  return $this;
63  }
64 
71  protected function getOptionsValue(): float
72  {
73  $product = $this->item->getProduct();
74  $value = 0.;
75  $basePrice = parent::getValue();
76  $optionIds = $this->item->getOptionByCode('option_ids');
77  if ($optionIds) {
78  foreach (explode(',', $optionIds->getValue()) as $optionId) {
79  $option = $product->getOptionById($optionId);
80  if ($option) {
81  $itemOption = $this->item->getOptionByCode('option_' . $option->getId());
83  $group = $option->groupFactory($option->getType())
84  ->setOption($option)
85  ->setConfigurationItem($this->item)
86  ->setConfigurationItemOption($itemOption);
87  $value += $group->getOptionPrice($itemOption->getValue(), $basePrice);
88  }
89  }
90  }
91 
92  return $value;
93  }
94 
100  public function getValue()
101  {
102  $basePrice = parent::getValue();
103 
104  return $this->item
105  ? $basePrice + $this->configuredOptions->getItemOptionsValue($basePrice, $this->item)
106  : $basePrice;
107  }
108 }
__construct(Product $saleableItem, $quantity, CalculatorInterface $calculator, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency, ItemInterface $item=null, ConfiguredOptions $configuredOptions=null)
$group
Definition: sections.phtml:16