Skip to content

Commit

Permalink
Merge branch '2.4-develop' into dcmm2020
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel da Gama authored Oct 10, 2020
2 parents ff5de32 + 6cd10d7 commit 1dab430
Show file tree
Hide file tree
Showing 19 changed files with 712 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

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

use Magento\Catalog\Pricing\Price\BasePrice;
use Magento\Catalog\Pricing\Price\CalculateCustomOptionCatalogRule;
use Magento\Catalog\Pricing\Price\CustomOptionPriceInterface;
use Magento\Framework\App\ObjectManager;

/**
* Product options section abstract block.
Expand Down Expand Up @@ -47,20 +50,29 @@ abstract class AbstractOptions extends \Magento\Framework\View\Element\Template
*/
protected $_catalogHelper;

/**
* @var CalculateCustomOptionCatalogRule
*/
private $calculateCustomOptionCatalogRule;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Framework\Pricing\Helper\Data $pricingHelper
* @param \Magento\Catalog\Helper\Data $catalogData
* @param array $data
* @param CalculateCustomOptionCatalogRule|null $calculateCustomOptionCatalogRule
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Pricing\Helper\Data $pricingHelper,
\Magento\Catalog\Helper\Data $catalogData,
array $data = []
array $data = [],
CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule = null
) {
$this->pricingHelper = $pricingHelper;
$this->_catalogHelper = $catalogData;
$this->calculateCustomOptionCatalogRule = $calculateCustomOptionCatalogRule
?? ObjectManager::getInstance()->get(CalculateCustomOptionCatalogRule::class);
parent::__construct($context, $data);
}

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

$customOptionPrice = $this->getProduct()->getPriceInfo()->getPrice('custom_option_price');
$isPercent = (bool) $value['is_percent'];

if (!$isPercent) {
$catalogPriceValue = $this->calculateCustomOptionCatalogRule->execute(
$this->getProduct(),
(float)$value['pricing_value'],
$isPercent
);
if ($catalogPriceValue !== null) {
$value['pricing_value'] = $catalogPriceValue;
}
}

$context = [CustomOptionPriceInterface::CONFIGURATION_OPTION_FLAG => true];
$optionAmount = $customOptionPrice->getCustomAmount($value['pricing_value'], null, $context);
$priceStr .= $this->getLayout()->getBlock('product.price.render.default')->renderAmount(
Expand Down
32 changes: 27 additions & 5 deletions app/code/Magento/Catalog/Model/Product/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
use Magento\Catalog\Model\Product\Option\Type\File;
use Magento\Catalog\Model\Product\Option\Type\Select;
use Magento\Catalog\Model\Product\Option\Type\Text;
use Magento\Catalog\Model\Product\Option\Value;
use Magento\Catalog\Model\ResourceModel\Product\Option\Value\Collection;
use Magento\Catalog\Pricing\Price\BasePrice;
use Magento\Catalog\Pricing\Price\CalculateCustomOptionCatalogRule;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Model\AbstractExtensibleModel;
Expand Down Expand Up @@ -123,6 +126,11 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
*/
private $customOptionValuesFactory;

/**
* @var CalculateCustomOptionCatalogRule
*/
private $calculateCustomOptionCatalogRule;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand All @@ -138,6 +146,7 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
* @param ProductCustomOptionValuesInterfaceFactory|null $customOptionValuesFactory
* @param array $optionGroups
* @param array $optionTypesToGroups
* @param CalculateCustomOptionCatalogRule|null $calculateCustomOptionCatalogRule
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -154,14 +163,17 @@ public function __construct(
array $data = [],
ProductCustomOptionValuesInterfaceFactory $customOptionValuesFactory = null,
array $optionGroups = [],
array $optionTypesToGroups = []
array $optionTypesToGroups = [],
CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule = null
) {
$this->productOptionValue = $productOptionValue;
$this->optionTypeFactory = $optionFactory;
$this->string = $string;
$this->validatorPool = $validatorPool;
$this->customOptionValuesFactory = $customOptionValuesFactory ?:
\Magento\Framework\App\ObjectManager::getInstance()->get(ProductCustomOptionValuesInterfaceFactory::class);
ObjectManager::getInstance()->get(ProductCustomOptionValuesInterfaceFactory::class);
$this->calculateCustomOptionCatalogRule = $calculateCustomOptionCatalogRule ??
ObjectManager::getInstance()->get(CalculateCustomOptionCatalogRule::class);
$this->optionGroups = $optionGroups ?: [
self::OPTION_GROUP_DATE => Date::class,
self::OPTION_GROUP_FILE => File::class,
Expand Down Expand Up @@ -462,11 +474,21 @@ public function afterSave()
*/
public function getPrice($flag = false)
{
if ($flag && $this->getPriceType() == self::$typePercent) {
$basePrice = $this->getProduct()->getPriceInfo()->getPrice(BasePrice::PRICE_CODE)->getValue();
$price = $basePrice * ($this->_getData(self::KEY_PRICE) / 100);
if ($flag && $this->getPriceType() === self::$typePercent) {
$price = $this->calculateCustomOptionCatalogRule->execute(
$this->getProduct(),
(float)$this->getData(self::KEY_PRICE),
$this->getPriceType() === Value::TYPE_PERCENT
);

if ($price === null) {
$basePrice = $this->getProduct()->getPriceInfo()->getPrice(BasePrice::PRICE_CODE)->getValue();
$price = $basePrice * ($this->_getData(self::KEY_PRICE) / 100);
}

return $price;
}

return $this->_getData(self::KEY_PRICE);
}

Expand Down
28 changes: 26 additions & 2 deletions app/code/Magento/Catalog/Model/Product/Option/Type/DefaultType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Magento\Catalog\Model\Product\Configuration\Item\Option\OptionInterface;
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
use Magento\Catalog\Model\Product\Option\Value;
use Magento\Catalog\Pricing\Price\CalculateCustomOptionCatalogRule;
use Magento\Framework\App\ObjectManager;

/**
* Catalog product option default type
Expand Down Expand Up @@ -60,21 +62,30 @@ class DefaultType extends \Magento\Framework\DataObject
*/
protected $_checkoutSession;

/**
* @var CalculateCustomOptionCatalogRule
*/
private $calculateCustomOptionCatalogRule;

/**
* Construct
*
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param array $data
* @param CalculateCustomOptionCatalogRule|null $calculateCustomOptionCatalogRule
*/
public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
array $data = []
array $data = [],
CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule = null
) {
$this->_checkoutSession = $checkoutSession;
parent::__construct($data);
$this->_scopeConfig = $scopeConfig;
$this->calculateCustomOptionCatalogRule = $calculateCustomOptionCatalogRule ?? ObjectManager::getInstance()
->get(CalculateCustomOptionCatalogRule::class);
}

/**
Expand Down Expand Up @@ -341,7 +352,20 @@ public function getOptionPrice($optionValue, $basePrice)
{
$option = $this->getOption();

return $this->_getChargeableOptionPrice($option->getPrice(), $option->getPriceType() == 'percent', $basePrice);
$catalogPriceValue = $this->calculateCustomOptionCatalogRule->execute(
$option->getProduct(),
(float)$option->getPrice(),
$option->getPriceType() === Value::TYPE_PERCENT
);
if ($catalogPriceValue !== null) {
return $catalogPriceValue;
} else {
return $this->_getChargeableOptionPrice(
$option->getPrice(),
$option->getPriceType() === Value::TYPE_PERCENT,
$basePrice
);
}
}

/**
Expand Down
65 changes: 55 additions & 10 deletions app/code/Magento/Catalog/Model/Product/Option/Type/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

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

use Magento\Catalog\Model\Product\Option\Value;
use Magento\Catalog\Pricing\Price\CalculateCustomOptionCatalogRule;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Catalog\Model\Product\Option;

/**
* Catalog product option select type
Expand Down Expand Up @@ -37,21 +41,28 @@ class Select extends \Magento\Catalog\Model\Product\Option\Type\DefaultType
*/
private $singleSelectionTypes;

/**
* @var CalculateCustomOptionCatalogRule
*/
private $calculateCustomOptionCatalogRule;

/**
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Framework\Stdlib\StringUtils $string
* @param \Magento\Framework\Escaper $escaper
* @param array $data
* @param array $singleSelectionTypes
* @param CalculateCustomOptionCatalogRule|null $calculateCustomOptionCatalogRule
*/
public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\Stdlib\StringUtils $string,
\Magento\Framework\Escaper $escaper,
array $data = [],
array $singleSelectionTypes = []
array $singleSelectionTypes = [],
CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule = null
) {
$this->string = $string;
$this->_escaper = $escaper;
Expand All @@ -61,6 +72,8 @@ public function __construct(
'drop_down' => \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN,
'radio' => \Magento\Catalog\Api\Data\ProductCustomOptionInterface::OPTION_TYPE_RADIO,
];
$this->calculateCustomOptionCatalogRule = $calculateCustomOptionCatalogRule ?? ObjectManager::getInstance()
->get(CalculateCustomOptionCatalogRule::class);
}

/**
Expand Down Expand Up @@ -248,11 +261,7 @@ public function getOptionPrice($optionValue, $basePrice)
foreach (explode(',', $optionValue) as $value) {
$_result = $option->getValueById($value);
if ($_result) {
$result += $this->_getChargeableOptionPrice(
$_result->getPrice(),
$_result->getPriceType() == 'percent',
$basePrice
);
$result += $this->getCalculatedOptionValue($option, $_result, $basePrice);
} else {
if ($this->getListener()) {
$this->getListener()->setHasError(true)->setMessage($this->_getWrongConfigurationMessage());
Expand All @@ -263,11 +272,20 @@ public function getOptionPrice($optionValue, $basePrice)
} elseif ($this->_isSingleSelection()) {
$_result = $option->getValueById($optionValue);
if ($_result) {
$result = $this->_getChargeableOptionPrice(
$_result->getPrice(),
$_result->getPriceType() == 'percent',
$basePrice
$catalogPriceValue = $this->calculateCustomOptionCatalogRule->execute(
$option->getProduct(),
(float)$_result->getPrice(),
$_result->getPriceType() === Value::TYPE_PERCENT
);
if ($catalogPriceValue !== null) {
$result = $catalogPriceValue;
} else {
$result = $this->_getChargeableOptionPrice(
$_result->getPrice(),
$_result->getPriceType() == 'percent',
$basePrice
);
}
} else {
if ($this->getListener()) {
$this->getListener()->setHasError(true)->setMessage($this->_getWrongConfigurationMessage());
Expand Down Expand Up @@ -329,4 +347,31 @@ protected function _isSingleSelection()
{
return in_array($this->getOption()->getType(), $this->singleSelectionTypes, true);
}

/**
* Returns calculated price of option
*
* @param Option $option
* @param Option\Value $result
* @param float $basePrice
* @return float
*/
protected function getCalculatedOptionValue(Option $option, Value $result, float $basePrice) : float
{
$catalogPriceValue = $this->calculateCustomOptionCatalogRule->execute(
$option->getProduct(),
(float)$result->getPrice(),
$result->getPriceType() === Value::TYPE_PERCENT
);
if ($catalogPriceValue !== null) {
$optionCalculatedValue = $catalogPriceValue;
} else {
$optionCalculatedValue = $this->_getChargeableOptionPrice(
$result->getPrice(),
$result->getPriceType() === Value::TYPE_PERCENT,
$basePrice
);
}
return $optionCalculatedValue;
}
}
26 changes: 23 additions & 3 deletions app/code/Magento/Catalog/Model/Product/Option/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Magento\Catalog\Model\Product\Option;
use Magento\Framework\Model\AbstractModel;
use Magento\Catalog\Pricing\Price\BasePrice;
use Magento\Catalog\Pricing\Price\CalculateCustomOptionCatalogRule;
use Magento\Framework\App\ObjectManager;
use Magento\Catalog\Pricing\Price\CustomOptionPriceCalculator;
use Magento\Catalog\Pricing\Price\RegularPrice;

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

/**
* @var CalculateCustomOptionCatalogRule
*/
private $calculateCustomOptionCatalogRule;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand All @@ -77,6 +84,7 @@ class Value extends AbstractModel implements \Magento\Catalog\Api\Data\ProductCu
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @param CustomOptionPriceCalculator|null $customOptionPriceCalculator
* @param CalculateCustomOptionCatalogRule|null $calculateCustomOptionCatalogRule
*/
public function __construct(
\Magento\Framework\Model\Context $context,
Expand All @@ -85,11 +93,14 @@ public function __construct(
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = [],
CustomOptionPriceCalculator $customOptionPriceCalculator = null
CustomOptionPriceCalculator $customOptionPriceCalculator = null,
CalculateCustomOptionCatalogRule $calculateCustomOptionCatalogRule = null
) {
$this->_valueCollectionFactory = $valueCollectionFactory;
$this->customOptionPriceCalculator = $customOptionPriceCalculator
?? \Magento\Framework\App\ObjectManager::getInstance()->get(CustomOptionPriceCalculator::class);
?? ObjectManager::getInstance()->get(CustomOptionPriceCalculator::class);
$this->calculateCustomOptionCatalogRule = $calculateCustomOptionCatalogRule
?? ObjectManager::getInstance()->get(CalculateCustomOptionCatalogRule::class);

parent::__construct(
$context,
Expand Down Expand Up @@ -253,7 +264,16 @@ public function saveValues()
public function getPrice($flag = false)
{
if ($flag) {
return $this->customOptionPriceCalculator->getOptionPriceByPriceCode($this, BasePrice::PRICE_CODE);
$catalogPriceValue = $this->calculateCustomOptionCatalogRule->execute(
$this->getProduct(),
(float)$this->getData(self::KEY_PRICE),
$this->getPriceType() === self::TYPE_PERCENT
);
if ($catalogPriceValue!==null) {
return $catalogPriceValue;
} else {
return $this->customOptionPriceCalculator->getOptionPriceByPriceCode($this, BasePrice::PRICE_CODE);
}
}
return $this->_getData(self::KEY_PRICE);
}
Expand Down
Loading

0 comments on commit 1dab430

Please sign in to comment.