Skip to content

Commit 1dab430

Browse files
author
Gabriel da Gama
authored
Merge branch '2.4-develop' into dcmm2020
2 parents ff5de32 + 6cd10d7 commit 1dab430

File tree

19 files changed

+712
-106
lines changed

19 files changed

+712
-106
lines changed

app/code/Magento/Catalog/Block/Product/View/Options/AbstractOptions.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212

1313
namespace Magento\Catalog\Block\Product\View\Options;
1414

15+
use Magento\Catalog\Pricing\Price\BasePrice;
16+
use Magento\Catalog\Pricing\Price\CalculateCustomOptionCatalogRule;
1517
use Magento\Catalog\Pricing\Price\CustomOptionPriceInterface;
18+
use Magento\Framework\App\ObjectManager;
1619

1720
/**
1821
* Product options section abstract block.
@@ -47,20 +50,29 @@ abstract class AbstractOptions extends \Magento\Framework\View\Element\Template
4750
*/
4851
protected $_catalogHelper;
4952

53+
/**
54+
* @var CalculateCustomOptionCatalogRule
55+
*/
56+
private $calculateCustomOptionCatalogRule;
57+
5058
/**
5159
* @param \Magento\Framework\View\Element\Template\Context $context
5260
* @param \Magento\Framework\Pricing\Helper\Data $pricingHelper
5361
* @param \Magento\Catalog\Helper\Data $catalogData
5462
* @param array $data
63+
* @param CalculateCustomOptionCatalogRule|null $calculateCustomOptionCatalogRule
5564
*/
5665
public function __construct(
5766
\Magento\Framework\View\Element\Template\Context $context,
5867
\Magento\Framework\Pricing\Helper\Data $pricingHelper,
5968
\Magento\Catalog\Helper\Data $catalogData,
60-
array $data = []
69+
array $data = [],
70+
CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule = null
6171
) {
6272
$this->pricingHelper = $pricingHelper;
6373
$this->_catalogHelper = $catalogData;
74+
$this->calculateCustomOptionCatalogRule = $calculateCustomOptionCatalogRule
75+
?? ObjectManager::getInstance()->get(CalculateCustomOptionCatalogRule::class);
6476
parent::__construct($context, $data);
6577
}
6678

@@ -162,6 +174,19 @@ protected function _formatPrice($value, $flag = true)
162174
$priceStr = $sign;
163175

164176
$customOptionPrice = $this->getProduct()->getPriceInfo()->getPrice('custom_option_price');
177+
$isPercent = (bool) $value['is_percent'];
178+
179+
if (!$isPercent) {
180+
$catalogPriceValue = $this->calculateCustomOptionCatalogRule->execute(
181+
$this->getProduct(),
182+
(float)$value['pricing_value'],
183+
$isPercent
184+
);
185+
if ($catalogPriceValue !== null) {
186+
$value['pricing_value'] = $catalogPriceValue;
187+
}
188+
}
189+
165190
$context = [CustomOptionPriceInterface::CONFIGURATION_OPTION_FLAG => true];
166191
$optionAmount = $customOptionPrice->getCustomAmount($value['pricing_value'], null, $context);
167192
$priceStr .= $this->getLayout()->getBlock('product.price.render.default')->renderAmount(

app/code/Magento/Catalog/Model/Product/Option.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
use Magento\Catalog\Model\Product\Option\Type\File;
1717
use Magento\Catalog\Model\Product\Option\Type\Select;
1818
use Magento\Catalog\Model\Product\Option\Type\Text;
19+
use Magento\Catalog\Model\Product\Option\Value;
1920
use Magento\Catalog\Model\ResourceModel\Product\Option\Value\Collection;
2021
use Magento\Catalog\Pricing\Price\BasePrice;
22+
use Magento\Catalog\Pricing\Price\CalculateCustomOptionCatalogRule;
23+
use Magento\Framework\App\ObjectManager;
2124
use Magento\Framework\EntityManager\MetadataPool;
2225
use Magento\Framework\Exception\LocalizedException;
2326
use Magento\Framework\Model\AbstractExtensibleModel;
@@ -123,6 +126,11 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
123126
*/
124127
private $customOptionValuesFactory;
125128

129+
/**
130+
* @var CalculateCustomOptionCatalogRule
131+
*/
132+
private $calculateCustomOptionCatalogRule;
133+
126134
/**
127135
* @param \Magento\Framework\Model\Context $context
128136
* @param \Magento\Framework\Registry $registry
@@ -138,6 +146,7 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
138146
* @param ProductCustomOptionValuesInterfaceFactory|null $customOptionValuesFactory
139147
* @param array $optionGroups
140148
* @param array $optionTypesToGroups
149+
* @param CalculateCustomOptionCatalogRule|null $calculateCustomOptionCatalogRule
141150
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
142151
*/
143152
public function __construct(
@@ -154,14 +163,17 @@ public function __construct(
154163
array $data = [],
155164
ProductCustomOptionValuesInterfaceFactory $customOptionValuesFactory = null,
156165
array $optionGroups = [],
157-
array $optionTypesToGroups = []
166+
array $optionTypesToGroups = [],
167+
CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule = null
158168
) {
159169
$this->productOptionValue = $productOptionValue;
160170
$this->optionTypeFactory = $optionFactory;
161171
$this->string = $string;
162172
$this->validatorPool = $validatorPool;
163173
$this->customOptionValuesFactory = $customOptionValuesFactory ?:
164-
\Magento\Framework\App\ObjectManager::getInstance()->get(ProductCustomOptionValuesInterfaceFactory::class);
174+
ObjectManager::getInstance()->get(ProductCustomOptionValuesInterfaceFactory::class);
175+
$this->calculateCustomOptionCatalogRule = $calculateCustomOptionCatalogRule ??
176+
ObjectManager::getInstance()->get(CalculateCustomOptionCatalogRule::class);
165177
$this->optionGroups = $optionGroups ?: [
166178
self::OPTION_GROUP_DATE => Date::class,
167179
self::OPTION_GROUP_FILE => File::class,
@@ -462,11 +474,21 @@ public function afterSave()
462474
*/
463475
public function getPrice($flag = false)
464476
{
465-
if ($flag && $this->getPriceType() == self::$typePercent) {
466-
$basePrice = $this->getProduct()->getPriceInfo()->getPrice(BasePrice::PRICE_CODE)->getValue();
467-
$price = $basePrice * ($this->_getData(self::KEY_PRICE) / 100);
477+
if ($flag && $this->getPriceType() === self::$typePercent) {
478+
$price = $this->calculateCustomOptionCatalogRule->execute(
479+
$this->getProduct(),
480+
(float)$this->getData(self::KEY_PRICE),
481+
$this->getPriceType() === Value::TYPE_PERCENT
482+
);
483+
484+
if ($price === null) {
485+
$basePrice = $this->getProduct()->getPriceInfo()->getPrice(BasePrice::PRICE_CODE)->getValue();
486+
$price = $basePrice * ($this->_getData(self::KEY_PRICE) / 100);
487+
}
488+
468489
return $price;
469490
}
491+
470492
return $this->_getData(self::KEY_PRICE);
471493
}
472494

app/code/Magento/Catalog/Model/Product/Option/Type/DefaultType.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface;
1414
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
1515
use Magento\Catalog\Model\Product\Option\Value;
16+
use Magento\Catalog\Pricing\Price\CalculateCustomOptionCatalogRule;
17+
use Magento\Framework\App\ObjectManager;
1618

1719
/**
1820
* Catalog product option default type
@@ -60,21 +62,30 @@ class DefaultType extends \Magento\Framework\DataObject
6062
*/
6163
protected $_checkoutSession;
6264

65+
/**
66+
* @var CalculateCustomOptionCatalogRule
67+
*/
68+
private $calculateCustomOptionCatalogRule;
69+
6370
/**
6471
* Construct
6572
*
6673
* @param \Magento\Checkout\Model\Session $checkoutSession
6774
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
6875
* @param array $data
76+
* @param CalculateCustomOptionCatalogRule|null $calculateCustomOptionCatalogRule
6977
*/
7078
public function __construct(
7179
\Magento\Checkout\Model\Session $checkoutSession,
7280
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
73-
array $data = []
81+
array $data = [],
82+
CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule = null
7483
) {
7584
$this->_checkoutSession = $checkoutSession;
7685
parent::__construct($data);
7786
$this->_scopeConfig = $scopeConfig;
87+
$this->calculateCustomOptionCatalogRule = $calculateCustomOptionCatalogRule ?? ObjectManager::getInstance()
88+
->get(CalculateCustomOptionCatalogRule::class);
7889
}
7990

8091
/**
@@ -341,7 +352,20 @@ public function getOptionPrice($optionValue, $basePrice)
341352
{
342353
$option = $this->getOption();
343354

344-
return $this->_getChargeableOptionPrice($option->getPrice(), $option->getPriceType() == 'percent', $basePrice);
355+
$catalogPriceValue = $this->calculateCustomOptionCatalogRule->execute(
356+
$option->getProduct(),
357+
(float)$option->getPrice(),
358+
$option->getPriceType() === Value::TYPE_PERCENT
359+
);
360+
if ($catalogPriceValue !== null) {
361+
return $catalogPriceValue;
362+
} else {
363+
return $this->_getChargeableOptionPrice(
364+
$option->getPrice(),
365+
$option->getPriceType() === Value::TYPE_PERCENT,
366+
$basePrice
367+
);
368+
}
345369
}
346370

347371
/**

app/code/Magento/Catalog/Model/Product/Option/Type/Select.php

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
namespace Magento\Catalog\Model\Product\Option\Type;
88

9+
use Magento\Catalog\Model\Product\Option\Value;
10+
use Magento\Catalog\Pricing\Price\CalculateCustomOptionCatalogRule;
11+
use Magento\Framework\App\ObjectManager;
912
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Catalog\Model\Product\Option;
1014

1115
/**
1216
* Catalog product option select type
@@ -37,21 +41,28 @@ class Select extends \Magento\Catalog\Model\Product\Option\Type\DefaultType
3741
*/
3842
private $singleSelectionTypes;
3943

44+
/**
45+
* @var CalculateCustomOptionCatalogRule
46+
*/
47+
private $calculateCustomOptionCatalogRule;
48+
4049
/**
4150
* @param \Magento\Checkout\Model\Session $checkoutSession
4251
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
4352
* @param \Magento\Framework\Stdlib\StringUtils $string
4453
* @param \Magento\Framework\Escaper $escaper
4554
* @param array $data
4655
* @param array $singleSelectionTypes
56+
* @param CalculateCustomOptionCatalogRule|null $calculateCustomOptionCatalogRule
4757
*/
4858
public function __construct(
4959
\Magento\Checkout\Model\Session $checkoutSession,
5060
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
5161
\Magento\Framework\Stdlib\StringUtils $string,
5262
\Magento\Framework\Escaper $escaper,
5363
array $data = [],
54-
array $singleSelectionTypes = []
64+
array $singleSelectionTypes = [],
65+
CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule = null
5566
) {
5667
$this->string = $string;
5768
$this->_escaper = $escaper;
@@ -61,6 +72,8 @@ public function __construct(
6172
'drop_down' => \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN,
6273
'radio' => \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_RADIO,
6374
];
75+
$this->calculateCustomOptionCatalogRule = $calculateCustomOptionCatalogRule ?? ObjectManager::getInstance()
76+
->get(CalculateCustomOptionCatalogRule::class);
6477
}
6578

6679
/**
@@ -248,11 +261,7 @@ public function getOptionPrice($optionValue, $basePrice)
248261
foreach (explode(',', $optionValue) as $value) {
249262
$_result = $option->getValueById($value);
250263
if ($_result) {
251-
$result += $this->_getChargeableOptionPrice(
252-
$_result->getPrice(),
253-
$_result->getPriceType() == 'percent',
254-
$basePrice
255-
);
264+
$result += $this->getCalculatedOptionValue($option, $_result, $basePrice);
256265
} else {
257266
if ($this->getListener()) {
258267
$this->getListener()->setHasError(true)->setMessage($this->_getWrongConfigurationMessage());
@@ -263,11 +272,20 @@ public function getOptionPrice($optionValue, $basePrice)
263272
} elseif ($this->_isSingleSelection()) {
264273
$_result = $option->getValueById($optionValue);
265274
if ($_result) {
266-
$result = $this->_getChargeableOptionPrice(
267-
$_result->getPrice(),
268-
$_result->getPriceType() == 'percent',
269-
$basePrice
275+
$catalogPriceValue = $this->calculateCustomOptionCatalogRule->execute(
276+
$option->getProduct(),
277+
(float)$_result->getPrice(),
278+
$_result->getPriceType() === Value::TYPE_PERCENT
270279
);
280+
if ($catalogPriceValue !== null) {
281+
$result = $catalogPriceValue;
282+
} else {
283+
$result = $this->_getChargeableOptionPrice(
284+
$_result->getPrice(),
285+
$_result->getPriceType() == 'percent',
286+
$basePrice
287+
);
288+
}
271289
} else {
272290
if ($this->getListener()) {
273291
$this->getListener()->setHasError(true)->setMessage($this->_getWrongConfigurationMessage());
@@ -329,4 +347,31 @@ protected function _isSingleSelection()
329347
{
330348
return in_array($this->getOption()->getType(), $this->singleSelectionTypes, true);
331349
}
350+
351+
/**
352+
* Returns calculated price of option
353+
*
354+
* @param Option $option
355+
* @param Option\Value $result
356+
* @param float $basePrice
357+
* @return float
358+
*/
359+
protected function getCalculatedOptionValue(Option $option, Value $result, float $basePrice) : float
360+
{
361+
$catalogPriceValue = $this->calculateCustomOptionCatalogRule->execute(
362+
$option->getProduct(),
363+
(float)$result->getPrice(),
364+
$result->getPriceType() === Value::TYPE_PERCENT
365+
);
366+
if ($catalogPriceValue !== null) {
367+
$optionCalculatedValue = $catalogPriceValue;
368+
} else {
369+
$optionCalculatedValue = $this->_getChargeableOptionPrice(
370+
$result->getPrice(),
371+
$result->getPriceType() === Value::TYPE_PERCENT,
372+
$basePrice
373+
);
374+
}
375+
return $optionCalculatedValue;
376+
}
332377
}

app/code/Magento/Catalog/Model/Product/Option/Value.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\Catalog\Model\Product\Option;
1111
use Magento\Framework\Model\AbstractModel;
1212
use Magento\Catalog\Pricing\Price\BasePrice;
13+
use Magento\Catalog\Pricing\Price\CalculateCustomOptionCatalogRule;
14+
use Magento\Framework\App\ObjectManager;
1315
use Magento\Catalog\Pricing\Price\CustomOptionPriceCalculator;
1416
use Magento\Catalog\Pricing\Price\RegularPrice;
1517

@@ -69,6 +71,11 @@ class Value extends AbstractModel implements \Magento\Catalog\Api\Data\ProductCu
6971
*/
7072
private $customOptionPriceCalculator;
7173

74+
/**
75+
* @var CalculateCustomOptionCatalogRule
76+
*/
77+
private $calculateCustomOptionCatalogRule;
78+
7279
/**
7380
* @param \Magento\Framework\Model\Context $context
7481
* @param \Magento\Framework\Registry $registry
@@ -77,6 +84,7 @@ class Value extends AbstractModel implements \Magento\Catalog\Api\Data\ProductCu
7784
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
7885
* @param array $data
7986
* @param CustomOptionPriceCalculator|null $customOptionPriceCalculator
87+
* @param CalculateCustomOptionCatalogRule|null $calculateCustomOptionCatalogRule
8088
*/
8189
public function __construct(
8290
\Magento\Framework\Model\Context $context,
@@ -85,11 +93,14 @@ public function __construct(
8593
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
8694
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
8795
array $data = [],
88-
CustomOptionPriceCalculator $customOptionPriceCalculator = null
96+
CustomOptionPriceCalculator $customOptionPriceCalculator = null,
97+
CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule = null
8998
) {
9099
$this->_valueCollectionFactory = $valueCollectionFactory;
91100
$this->customOptionPriceCalculator = $customOptionPriceCalculator
92-
?? \Magento\Framework\App\ObjectManager::getInstance()->get(CustomOptionPriceCalculator::class);
101+
?? ObjectManager::getInstance()->get(CustomOptionPriceCalculator::class);
102+
$this->calculateCustomOptionCatalogRule = $calculateCustomOptionCatalogRule
103+
?? ObjectManager::getInstance()->get(CalculateCustomOptionCatalogRule::class);
93104

94105
parent::__construct(
95106
$context,
@@ -253,7 +264,16 @@ public function saveValues()
253264
public function getPrice($flag = false)
254265
{
255266
if ($flag) {
256-
return $this->customOptionPriceCalculator->getOptionPriceByPriceCode($this, BasePrice::PRICE_CODE);
267+
$catalogPriceValue = $this->calculateCustomOptionCatalogRule->execute(
268+
$this->getProduct(),
269+
(float)$this->getData(self::KEY_PRICE),
270+
$this->getPriceType() === self::TYPE_PERCENT
271+
);
272+
if ($catalogPriceValue!==null) {
273+
return $catalogPriceValue;
274+
} else {
275+
return $this->customOptionPriceCalculator->getOptionPriceByPriceCode($this, BasePrice::PRICE_CODE);
276+
}
257277
}
258278
return $this->_getData(self::KEY_PRICE);
259279
}

0 commit comments

Comments
 (0)