33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
67
78namespace Magento \Catalog \Model \Product \Option \Type ;
89
9- use Magento \Framework \Exception \LocalizedException ;
1010use Magento \Catalog \Api \Data \ProductCustomOptionInterface ;
11+ use Magento \Catalog \Model \Product ;
12+ use Magento \Catalog \Model \Product \Configuration \Item \ItemInterface ;
13+ use Magento \Catalog \Model \Product \Configuration \Item \Option \OptionInterface ;
14+ use Magento \Catalog \Model \Product \Option ;
15+ use Magento \Catalog \Model \Product \Option \Value ;
16+ use Magento \Catalog \Pricing \Price \CalculateCustomOptionCatalogRule ;
17+ use Magento \Framework \App \ObjectManager ;
18+ use Magento \Framework \Exception \LocalizedException ;
1119
1220/**
1321 * Catalog product option default type
1422 *
1523 * @api
1624 * @author Magento Core Team <[email protected] > 25+ * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1726 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1827 * @since 100.0.2
1928 */
@@ -22,14 +31,14 @@ class DefaultType extends \Magento\Framework\DataObject
2231 /**
2332 * Option Instance
2433 *
25- * @var \Magento\Catalog\Model\Product\ Option
34+ * @var Option
2635 */
2736 protected $ _option ;
2837
2938 /**
3039 * Product Instance
3140 *
32- * @var \Magento\Catalog\Model\ Product
41+ * @var Product
3342 */
3443 protected $ _product ;
3544
@@ -54,27 +63,36 @@ class DefaultType extends \Magento\Framework\DataObject
5463 */
5564 protected $ _checkoutSession ;
5665
66+ /**
67+ * @var CalculateCustomOptionCatalogRule
68+ */
69+ private $ calculateCustomOptionCatalogRule ;
70+
5771 /**
5872 * Construct
5973 *
6074 * @param \Magento\Checkout\Model\Session $checkoutSession
6175 * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
6276 * @param array $data
77+ * @param CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule
6378 */
6479 public function __construct (
6580 \Magento \Checkout \Model \Session $ checkoutSession ,
6681 \Magento \Framework \App \Config \ScopeConfigInterface $ scopeConfig ,
67- array $ data = []
82+ array $ data = [],
83+ CalculateCustomOptionCatalogRule $ calculateCustomOptionCatalogRule = null
6884 ) {
6985 $ this ->_checkoutSession = $ checkoutSession ;
7086 parent ::__construct ($ data );
7187 $ this ->_scopeConfig = $ scopeConfig ;
88+ $ this ->calculateCustomOptionCatalogRule = $ calculateCustomOptionCatalogRule ?? ObjectManager::getInstance ()
89+ ->get (CalculateCustomOptionCatalogRule::class);
7290 }
7391
7492 /**
7593 * Option Instance setter
7694 *
77- * @param \Magento\Catalog\Model\Product\ Option $option
95+ * @param Option $option
7896 * @return $this
7997 */
8098 public function setOption ($ option )
@@ -86,12 +104,12 @@ public function setOption($option)
86104 /**
87105 * Option Instance getter
88106 *
107+ * @return Option
89108 * @throws \Magento\Framework\Exception\LocalizedException
90- * @return \Magento\Catalog\Model\Product\Option
91109 */
92110 public function getOption ()
93111 {
94- if ($ this ->_option instanceof \ Magento \ Catalog \ Model \ Product \ Option) {
112+ if ($ this ->_option instanceof Option) {
95113 return $ this ->_option ;
96114 }
97115 throw new LocalizedException (__ ('The option instance type in options group is incorrect. ' ));
@@ -100,7 +118,7 @@ public function getOption()
100118 /**
101119 * Product Instance setter
102120 *
103- * @param \Magento\Catalog\Model\ Product $product
121+ * @param Product $product
104122 * @return $this
105123 */
106124 public function setProduct ($ product )
@@ -112,12 +130,12 @@ public function setProduct($product)
112130 /**
113131 * Product Instance getter
114132 *
133+ * @return Product
115134 * @throws \Magento\Framework\Exception\LocalizedException
116- * @return \Magento\Catalog\Model\Product
117135 */
118136 public function getProduct ()
119137 {
120- if ($ this ->_product instanceof \ Magento \ Catalog \ Model \ Product) {
138+ if ($ this ->_product instanceof Product) {
121139 return $ this ->_product ;
122140 }
123141 throw new LocalizedException (__ ('The product instance type in options group is incorrect. ' ));
@@ -126,15 +144,12 @@ public function getProduct()
126144 /**
127145 * Getter for Configuration Item Option
128146 *
129- * @return \Magento\Catalog\Model\Product\Configuration\Item\Option\ OptionInterface
147+ * @return OptionInterface
130148 * @throws LocalizedException
131149 */
132150 public function getConfigurationItemOption ()
133151 {
134- if ($ this ->_getData (
135- 'configuration_item_option '
136- ) instanceof \Magento \Catalog \Model \Product \Configuration \Item \Option \OptionInterface
137- ) {
152+ if ($ this ->_getData ('configuration_item_option ' ) instanceof OptionInterface) {
138153 return $ this ->_getData ('configuration_item_option ' );
139154 }
140155
@@ -149,14 +164,12 @@ public function getConfigurationItemOption()
149164 /**
150165 * Getter for Configuration Item
151166 *
152- * @return \Magento\Catalog\Model\Product\Configuration\Item\ ItemInterface
167+ * @return ItemInterface
153168 * @throws \Magento\Framework\Exception\LocalizedException
154169 */
155170 public function getConfigurationItem ()
156171 {
157- if ($ this ->_getData (
158- 'configuration_item '
159- ) instanceof \Magento \Catalog \Model \Product \Configuration \Item \ItemInterface
172+ if ($ this ->_getData ('configuration_item ' ) instanceof ItemInterface
160173 ) {
161174 return $ this ->_getData ('configuration_item ' );
162175 }
@@ -341,7 +354,11 @@ public function getOptionPrice($optionValue, $basePrice)
341354 {
342355 $ option = $ this ->getOption ();
343356
344- return $ this ->_getChargeableOptionPrice ($ option ->getPrice (), $ option ->getPriceType () == 'percent ' , $ basePrice );
357+ return $ this ->calculateCustomOptionCatalogRule ->execute (
358+ $ option ->getProduct (),
359+ (float )$ option ->getPrice (),
360+ $ option ->getPriceType () === Value::TYPE_PERCENT
361+ );
345362 }
346363
347364 /**
@@ -368,14 +385,14 @@ public function getProductOptions()
368385 $ options = $ this ->getProduct ()->getOptions ();
369386 if ($ options != null ) {
370387 foreach ($ options as $ _option ) {
371- /* @var $option \Magento\Catalog\Model\Product\ Option */
388+ /* @var $option Option */
372389 $ this ->_productOptions [$ this ->getProduct ()->getId ()][$ _option ->getTitle ()] = [
373390 'option_id ' => $ _option ->getId (),
374391 ];
375392 if ($ _option ->getGroupByType () == ProductCustomOptionInterface::OPTION_GROUP_SELECT ) {
376393 $ optionValues = [];
377394 foreach ($ _option ->getValues () as $ _value ) {
378- /* @var $value \Magento\Catalog\Model\Product\Option\ Value */
395+ /* @var $value Value */
379396 $ optionValues [$ _value ->getTitle ()] = $ _value ->getId ();
380397 }
381398 $ this ->_productOptions [$ this
@@ -395,12 +412,14 @@ public function getProductOptions()
395412 }
396413
397414 /**
415+ * Return final chargeable price for option
416+ *
398417 * @param float $price Price of option
399418 * @param boolean $isPercent Price type - percent or fixed
400419 * @param float $basePrice For percent price type
401420 * @return float
402421 * @deprecated 102.0.4 typo in method name
403- * @see _getChargeableOptionPrice
422+ * @see CalculateCustomOptionCatalogRule::execute
404423 */
405424 protected function _getChargableOptionPrice ($ price , $ isPercent , $ basePrice )
406425 {
@@ -414,6 +433,8 @@ protected function _getChargableOptionPrice($price, $isPercent, $basePrice)
414433 * @param boolean $isPercent Price type - percent or fixed
415434 * @param float $basePrice For percent price type
416435 * @return float
436+ * @deprecated
437+ * @see CalculateCustomOptionCatalogRule::execute
417438 */
418439 protected function _getChargeableOptionPrice ($ price , $ isPercent , $ basePrice )
419440 {
0 commit comments