Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomOptionPrice.php
Go to the documentation of this file.
1 <?php
7 
14 
20 {
24  const PRICE_CODE = 'custom_option_price';
25 
29  protected $priceOptions;
30 
36  protected $excludeAdjustment = null;
37 
41  private $customOptionPriceCalculator;
42 
51  public function __construct(
52  SaleableInterface $saleableItem,
53  $quantity,
56  $excludeAdjustment = null,
57  CustomOptionPriceCalculator $customOptionPriceCalculator = null
58  ) {
59  parent::__construct($saleableItem, $quantity, $calculator, $priceCurrency);
60  $this->excludeAdjustment = $excludeAdjustment;
61  $this->customOptionPriceCalculator = $customOptionPriceCalculator
62  ?? \Magento\Framework\App\ObjectManager::getInstance()->get(CustomOptionPriceCalculator::class);
63  }
64 
73  public function getValue($priceCode = \Magento\Catalog\Pricing\Price\BasePrice::PRICE_CODE)
74  {
75  $optionValues = [];
76  $options = $this->product->getOptions();
77  if ($options) {
79  foreach ($options as $optionItem) {
80  $min = null;
81  if (!$optionItem->getIsRequire()) {
82  $min = 0.;
83  }
84  $max = 0.;
85  if ($optionItem->getValues() === null && $optionItem->getPrice() !== null) {
86  $price = $optionItem->getPrice($optionItem->getPriceType() == Value::TYPE_PERCENT);
87  if ($min === null) {
88  $min = $price;
89  } elseif ($price < $min) {
90  $min = $price;
91  }
92  if ($price > $max) {
93  $max = $price;
94  }
95  } else {
97  foreach ($optionItem->getValues() as $optionValue) {
98  $price =
99  $this->customOptionPriceCalculator->getOptionPriceByPriceCode($optionValue, $priceCode);
100  if ($min === null) {
101  $min = $price;
102  } elseif ($price < $min) {
103  $min = $price;
104  }
105  $type = $optionItem->getType();
108  ) {
109  $max += $price;
110  } elseif ($price > $max) {
111  $max = $price;
112  }
113  }
114  }
115  $optionValues[] = [
116  'option_id' => $optionItem->getId(),
117  'type' => $optionItem->getType(),
118  'min' => ($min === null) ? 0. : $min,
119  'max' => $max,
120  ];
121  }
122  }
123  return $optionValues;
124  }
125 
132  public function getCustomAmount($amount = null, $exclude = null, $context = [])
133  {
134  if (null !== $amount) {
135  $amount = $this->priceCurrency->convertAndRound($amount);
136  } else {
137  $amount = $this->getValue();
138  }
139  $exclude = $this->excludeAdjustment;
140  return $this->calculator->getAmount($amount, $this->getProduct(), $exclude, $context);
141  }
142 
150  public function getCustomOptionRange($getMin, $priceCode = \Magento\Catalog\Pricing\Price\BasePrice::PRICE_CODE)
151  {
152  $optionValue = 0.;
153  $options = $this->getValue($priceCode);
154  foreach ($options as $option) {
155  if ($getMin) {
156  $optionValue += $option['min'];
157  } else {
158  $optionValue += $option['max'];
159  }
160  }
161  return $this->priceCurrency->convertAndRound($optionValue);
162  }
163 
169  public function getSelectedOptions()
170  {
171  if (null !== $this->value) {
172  return $this->value;
173  }
174  $this->value = false;
175  $optionIds = $this->product->getCustomOption('option_ids');
176  if (!$optionIds) {
177  return $this->value;
178  }
179  $this->value = 0.;
180 
181  if ($optionIds) {
182  $values = explode(',', $optionIds->getValue());
183  $values = array_filter($values);
184  if (!empty($values)) {
185  $this->value = $this->processOptions($values);
186  }
187  }
188  return $this->value;
189  }
190 
197  protected function processOptions(array $values)
198  {
199  $value = 0.;
200  foreach ($values as $optionId) {
201  $option = $this->product->getOptionById($optionId);
202  if (!$option) {
203  continue;
204  }
205  $confItemOption = $this->product->getCustomOption('option_' . $option->getId());
206 
207  $group = $option->groupFactory($option->getType())
208  ->setOption($option)
209  ->setConfigurationItemOption($confItemOption);
210  $value += $group->getOptionPrice($confItemOption->getValue(), $this->value);
211  }
212  return $value;
213  }
214 
220  public function getOptions()
221  {
222  if (null !== $this->priceOptions) {
223  return $this->priceOptions;
224  }
225  $this->priceOptions = [];
226  $options = $this->product->getOptions();
227  if ($options) {
229  foreach ($options as $optionItem) {
231  foreach ($optionItem->getValues() as $optionValue) {
232  $price = $optionValue->getPrice($optionValue->getPriceType() == Value::TYPE_PERCENT);
233  $this->priceOptions[$optionValue->getId()][$price] = [
234  'base_amount' => $price,
235  'adjustment' => $this->getCustomAmount($price)->getValue(),
236  ];
237  }
238  }
239  }
240  return $this->priceOptions;
241  }
242 }
__construct(SaleableInterface $saleableItem, $quantity, CalculatorInterface $calculator, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency, $excludeAdjustment=null, CustomOptionPriceCalculator $customOptionPriceCalculator=null)
$block setTitle( 'CMS Block Title') -> setIdentifier('fixture_block') ->setContent('< h1 >Fixture Block Title</h1 >< a href=" store url</a><p> Config value
Definition: block.php:9
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$group
Definition: sections.phtml:16
$values
Definition: options.phtml:88
$price
$type
Definition: item.phtml:13
getCustomAmount($amount=null, $exclude=null, $context=[])
getCustomOptionRange($getMin, $priceCode=\Magento\Catalog\Pricing\Price\BasePrice::PRICE_CODE)