Skip to content

Commit a9222ed

Browse files
[Magento Community Engineering] Community Contributions - 2.4-develop-fast-lane-prs
- merged with '2.4-develop-temporary-one-prs' branch
2 parents 69b99f1 + d86c358 commit a9222ed

File tree

19 files changed

+756
-124
lines changed

19 files changed

+756
-124
lines changed

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
7-
/**
8-
* Product options abstract type block
9-
*
10-
* @author Magento Core Team <core@magentocommerce.com>
11-
*/
6+
declare(strict_types=1);
127

138
namespace Magento\Catalog\Block\Product\View\Options;
149

10+
use Magento\Catalog\Pricing\Price\BasePrice;
11+
use Magento\Catalog\Pricing\Price\CalculateCustomOptionCatalogRule;
1512
use Magento\Catalog\Pricing\Price\CustomOptionPriceInterface;
13+
use Magento\Framework\App\ObjectManager;
1614

1715
/**
1816
* Product options section abstract block.
@@ -47,20 +45,29 @@ abstract class AbstractOptions extends \Magento\Framework\View\Element\Template
4745
*/
4846
protected $_catalogHelper;
4947

48+
/**
49+
* @var CalculateCustomOptionCatalogRule
50+
*/
51+
private $calculateCustomOptionCatalogRule;
52+
5053
/**
5154
* @param \Magento\Framework\View\Element\Template\Context $context
5255
* @param \Magento\Framework\Pricing\Helper\Data $pricingHelper
5356
* @param \Magento\Catalog\Helper\Data $catalogData
5457
* @param array $data
58+
* @param CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule
5559
*/
5660
public function __construct(
5761
\Magento\Framework\View\Element\Template\Context $context,
5862
\Magento\Framework\Pricing\Helper\Data $pricingHelper,
5963
\Magento\Catalog\Helper\Data $catalogData,
60-
array $data = []
64+
array $data = [],
65+
CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule = null
6166
) {
6267
$this->pricingHelper = $pricingHelper;
6368
$this->_catalogHelper = $catalogData;
69+
$this->calculateCustomOptionCatalogRule = $calculateCustomOptionCatalogRule
70+
?? ObjectManager::getInstance()->get(CalculateCustomOptionCatalogRule::class);
6471
parent::__construct($context, $data);
6572
}
6673

@@ -161,6 +168,15 @@ protected function _formatPrice($value, $flag = true)
161168
$priceStr = $sign;
162169

163170
$customOptionPrice = $this->getProduct()->getPriceInfo()->getPrice('custom_option_price');
171+
172+
if (!$value['is_percent']) {
173+
$value['pricing_value'] = $this->calculateCustomOptionCatalogRule->execute(
174+
$this->getProduct(),
175+
(float)$value['pricing_value'],
176+
(bool)$value['is_percent']
177+
);
178+
}
179+
164180
$context = [CustomOptionPriceInterface::CONFIGURATION_OPTION_FLAG => true];
165181
$optionAmount = $customOptionPrice->getCustomAmount($value['pricing_value'], null, $context);
166182
$priceStr .= $this->getLayout()->getBlock('product.price.render.default')->renderAmount(

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Catalog\Model\Product;
89

@@ -16,8 +17,10 @@
1617
use Magento\Catalog\Model\Product\Option\Type\File;
1718
use Magento\Catalog\Model\Product\Option\Type\Select;
1819
use Magento\Catalog\Model\Product\Option\Type\Text;
20+
use Magento\Catalog\Model\Product\Option\Value;
1921
use Magento\Catalog\Model\ResourceModel\Product\Option\Value\Collection;
20-
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 $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,10 +474,12 @@ 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);
468-
return $price;
477+
if ($flag) {
478+
return $this->calculateCustomOptionCatalogRule->execute(
479+
$this->getProduct(),
480+
(float)$this->getData(self::KEY_PRICE),
481+
$this->getPriceType() === Value::TYPE_PERCENT
482+
);
469483
}
470484
return $this->_getData(self::KEY_PRICE);
471485
}
@@ -952,7 +966,7 @@ public function setExtensionAttributes(
952966
private function getOptionRepository()
953967
{
954968
if (null === $this->optionRepository) {
955-
$this->optionRepository = \Magento\Framework\App\ObjectManager::getInstance()
969+
$this->optionRepository = ObjectManager::getInstance()
956970
->get(\Magento\Catalog\Model\Product\Option\Repository::class);
957971
}
958972
return $this->optionRepository;
@@ -966,7 +980,7 @@ private function getOptionRepository()
966980
private function getMetadataPool()
967981
{
968982
if (null === $this->metadataPool) {
969-
$this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
983+
$this->metadataPool = ObjectManager::getInstance()
970984
->get(\Magento\Framework\EntityManager\MetadataPool::class);
971985
}
972986
return $this->metadataPool;

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

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Catalog\Model\Product\Option\Type;
89

9-
use Magento\Framework\Exception\LocalizedException;
1010
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;
1119

1220
/**
1321
* Catalog product option default type
1422
*
1523
* @api
1624
* @author Magento Core Team <core@magentocommerce.com>
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

Comments
 (0)