3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
6
7
7
8
namespace Magento \Catalog \Model \Product \Option \Type ;
8
9
9
- use Magento \Framework \Exception \LocalizedException ;
10
10
use 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 ;
11
19
12
20
/**
13
21
* Catalog product option default type
14
22
*
15
23
* @api
16
24
* @author Magento Core Team <core@magentocommerce.com>
25
+ * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
17
26
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18
27
* @since 100.0.2
19
28
*/
@@ -22,14 +31,14 @@ class DefaultType extends \Magento\Framework\DataObject
22
31
/**
23
32
* Option Instance
24
33
*
25
- * @var \Magento\Catalog\Model\Product\ Option
34
+ * @var Option
26
35
*/
27
36
protected $ _option ;
28
37
29
38
/**
30
39
* Product Instance
31
40
*
32
- * @var \Magento\Catalog\Model\ Product
41
+ * @var Product
33
42
*/
34
43
protected $ _product ;
35
44
@@ -54,27 +63,36 @@ class DefaultType extends \Magento\Framework\DataObject
54
63
*/
55
64
protected $ _checkoutSession ;
56
65
66
+ /**
67
+ * @var CalculateCustomOptionCatalogRule
68
+ */
69
+ private $ calculateCustomOptionCatalogRule ;
70
+
57
71
/**
58
72
* Construct
59
73
*
60
74
* @param \Magento\Checkout\Model\Session $checkoutSession
61
75
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
62
76
* @param array $data
77
+ * @param CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule
63
78
*/
64
79
public function __construct (
65
80
\Magento \Checkout \Model \Session $ checkoutSession ,
66
81
\Magento \Framework \App \Config \ScopeConfigInterface $ scopeConfig ,
67
- array $ data = []
82
+ array $ data = [],
83
+ CalculateCustomOptionCatalogRule $ calculateCustomOptionCatalogRule = null
68
84
) {
69
85
$ this ->_checkoutSession = $ checkoutSession ;
70
86
parent ::__construct ($ data );
71
87
$ this ->_scopeConfig = $ scopeConfig ;
88
+ $ this ->calculateCustomOptionCatalogRule = $ calculateCustomOptionCatalogRule ?? ObjectManager::getInstance ()
89
+ ->get (CalculateCustomOptionCatalogRule::class);
72
90
}
73
91
74
92
/**
75
93
* Option Instance setter
76
94
*
77
- * @param \Magento\Catalog\Model\Product\ Option $option
95
+ * @param Option $option
78
96
* @return $this
79
97
*/
80
98
public function setOption ($ option )
@@ -86,12 +104,12 @@ public function setOption($option)
86
104
/**
87
105
* Option Instance getter
88
106
*
107
+ * @return Option
89
108
* @throws \Magento\Framework\Exception\LocalizedException
90
- * @return \Magento\Catalog\Model\Product\Option
91
109
*/
92
110
public function getOption ()
93
111
{
94
- if ($ this ->_option instanceof \ Magento \ Catalog \ Model \ Product \ Option) {
112
+ if ($ this ->_option instanceof Option) {
95
113
return $ this ->_option ;
96
114
}
97
115
throw new LocalizedException (__ ('The option instance type in options group is incorrect. ' ));
@@ -100,7 +118,7 @@ public function getOption()
100
118
/**
101
119
* Product Instance setter
102
120
*
103
- * @param \Magento\Catalog\Model\ Product $product
121
+ * @param Product $product
104
122
* @return $this
105
123
*/
106
124
public function setProduct ($ product )
@@ -112,12 +130,12 @@ public function setProduct($product)
112
130
/**
113
131
* Product Instance getter
114
132
*
133
+ * @return Product
115
134
* @throws \Magento\Framework\Exception\LocalizedException
116
- * @return \Magento\Catalog\Model\Product
117
135
*/
118
136
public function getProduct ()
119
137
{
120
- if ($ this ->_product instanceof \ Magento \ Catalog \ Model \ Product) {
138
+ if ($ this ->_product instanceof Product) {
121
139
return $ this ->_product ;
122
140
}
123
141
throw new LocalizedException (__ ('The product instance type in options group is incorrect. ' ));
@@ -126,15 +144,12 @@ public function getProduct()
126
144
/**
127
145
* Getter for Configuration Item Option
128
146
*
129
- * @return \Magento\Catalog\Model\Product\Configuration\Item\Option\ OptionInterface
147
+ * @return OptionInterface
130
148
* @throws LocalizedException
131
149
*/
132
150
public function getConfigurationItemOption ()
133
151
{
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) {
138
153
return $ this ->_getData ('configuration_item_option ' );
139
154
}
140
155
@@ -149,14 +164,12 @@ public function getConfigurationItemOption()
149
164
/**
150
165
* Getter for Configuration Item
151
166
*
152
- * @return \Magento\Catalog\Model\Product\Configuration\Item\ ItemInterface
167
+ * @return ItemInterface
153
168
* @throws \Magento\Framework\Exception\LocalizedException
154
169
*/
155
170
public function getConfigurationItem ()
156
171
{
157
- if ($ this ->_getData (
158
- 'configuration_item '
159
- ) instanceof \Magento \Catalog \Model \Product \Configuration \Item \ItemInterface
172
+ if ($ this ->_getData ('configuration_item ' ) instanceof ItemInterface
160
173
) {
161
174
return $ this ->_getData ('configuration_item ' );
162
175
}
@@ -341,7 +354,11 @@ public function getOptionPrice($optionValue, $basePrice)
341
354
{
342
355
$ option = $ this ->getOption ();
343
356
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
+ );
345
362
}
346
363
347
364
/**
@@ -368,14 +385,14 @@ public function getProductOptions()
368
385
$ options = $ this ->getProduct ()->getOptions ();
369
386
if ($ options != null ) {
370
387
foreach ($ options as $ _option ) {
371
- /* @var $option \Magento\Catalog\Model\Product\ Option */
388
+ /* @var $option Option */
372
389
$ this ->_productOptions [$ this ->getProduct ()->getId ()][$ _option ->getTitle ()] = [
373
390
'option_id ' => $ _option ->getId (),
374
391
];
375
392
if ($ _option ->getGroupByType () == ProductCustomOptionInterface::OPTION_GROUP_SELECT ) {
376
393
$ optionValues = [];
377
394
foreach ($ _option ->getValues () as $ _value ) {
378
- /* @var $value \Magento\Catalog\Model\Product\Option\ Value */
395
+ /* @var $value Value */
379
396
$ optionValues [$ _value ->getTitle ()] = $ _value ->getId ();
380
397
}
381
398
$ this ->_productOptions [$ this
@@ -395,12 +412,14 @@ public function getProductOptions()
395
412
}
396
413
397
414
/**
415
+ * Return final chargeable price for option
416
+ *
398
417
* @param float $price Price of option
399
418
* @param boolean $isPercent Price type - percent or fixed
400
419
* @param float $basePrice For percent price type
401
420
* @return float
402
421
* @deprecated 102.0.4 typo in method name
403
- * @see _getChargeableOptionPrice
422
+ * @see CalculateCustomOptionCatalogRule::execute
404
423
*/
405
424
protected function _getChargableOptionPrice ($ price , $ isPercent , $ basePrice )
406
425
{
@@ -414,6 +433,8 @@ protected function _getChargableOptionPrice($price, $isPercent, $basePrice)
414
433
* @param boolean $isPercent Price type - percent or fixed
415
434
* @param float $basePrice For percent price type
416
435
* @return float
436
+ * @deprecated
437
+ * @see CalculateCustomOptionCatalogRule::execute
417
438
*/
418
439
protected function _getChargeableOptionPrice ($ price , $ isPercent , $ basePrice )
419
440
{
0 commit comments