Skip to content

Adds Catalog Price Rules to Custom Options #38464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ protected function _formatPrice($value, $flag = true)
$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 = $isPercent
? $this->calculator->getAmount(
Expand Down
29 changes: 25 additions & 4 deletions app/code/Magento/Catalog/Model/Product/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
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;
Expand Down Expand Up @@ -128,6 +130,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 Down Expand Up @@ -159,14 +166,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 ?:
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 @@ -468,10 +478,21 @@ public function afterSave()
*/
public function getPrice($flag = false)
{
if ($flag && $this->getPriceType() == self::$typePercent) {
$basePrice = $this->getProduct()->getPriceInfo()->getPrice(BasePrice::PRICE_CODE)->getValue();
return $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
30 changes: 25 additions & 5 deletions app/code/Magento/Catalog/Model/Product/Option/Type/DefaultType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Magento\Catalog\Model\Product\Configuration\Item\ItemInterface;
use Magento\Catalog\Model\Product\Option\Value;
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
use Magento\Catalog\Pricing\Price\CalculateCustomOptionCatalogRule;
use Magento\Framework\App\ObjectManager;

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

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

/**
* Construct
*
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param CalculateCustomOptionCatalogRule|null $calculateCustomOptionCatalogRule
* @param array $data
*/
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 @@ -338,11 +349,20 @@ public function getOptionPrice($optionValue, $basePrice)
{
$option = $this->getOption();

return $this->_getChargeableOptionPrice(
$option->getPrice(),
$option->getPriceType() === Value::TYPE_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 @@ -7,7 +7,10 @@
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 @@ -38,6 +41,11 @@ 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
Expand All @@ -52,7 +60,8 @@ public function __construct(
\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 @@ -62,6 +71,10 @@ 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 @@ -253,11 +266,7 @@ public function getOptionPrice($optionValue, $basePrice)
foreach (explode(',', (string)$optionValue) as $value) {
$_result = $option->getValueById($value);
if ($_result) {
$result += $this->_getChargeableOptionPrice(
$_result->getPrice(),
$_result->getPriceType() === Value::TYPE_PERCENT,
$basePrice
);
$result += $this->getCalculatedOptionValue($option, $_result, $basePrice);
} else {
if ($this->getListener()) {
$this->getListener()->setHasError(true)->setMessage($this->_getWrongConfigurationMessage());
Expand All @@ -268,11 +277,20 @@ public function getOptionPrice($optionValue, $basePrice)
} elseif ($this->_isSingleSelection()) {
$_result = $option->getValueById($optionValue);
if ($_result) {
$result = $this->_getChargeableOptionPrice(
$_result->getPrice(),
$_result->getPriceType() === Value::TYPE_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 @@ -334,4 +352,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;
}
}
25 changes: 22 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,7 @@
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\Catalog\Pricing\Price\CustomOptionPriceCalculator;
use Magento\Catalog\Pricing\Price\RegularPrice;
use Magento\Framework\App\ObjectManager;
Expand Down Expand Up @@ -70,14 +71,20 @@ 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
* @param \Magento\Catalog\Model\ResourceModel\Product\Option\Value\CollectionFactory $valueCollectionFactory
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @param CustomOptionPriceCalculator|null $customOptionPriceCalculator
* @param CalculateCustomOptionCatalogRule|null $calculateCustomOptionCatalogRule
* @param array $data
*/
public function __construct(
\Magento\Framework\Model\Context $context,
Expand All @@ -86,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
?? ObjectManager::getInstance()->get(CustomOptionPriceCalculator::class);
$this->calculateCustomOptionCatalogRule = $calculateCustomOptionCatalogRule
?? ObjectManager::getInstance()->get(CalculateCustomOptionCatalogRule::class);

parent::__construct(
$context,
Expand Down Expand Up @@ -254,7 +264,16 @@ public function saveValues()
public function getPrice($flag = false)
{
if ($flag) {
return $this->customOptionPriceCalculator->getOptionPriceByPriceCode($this);
$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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\PriceModifierInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;

/**
Expand All @@ -20,25 +21,25 @@
class CalculateCustomOptionCatalogRule
{
/**
* @var PriceCurrencyInterface
* @var PriceModifierInterface
*/
private $priceCurrency;
private $priceModifier;

/**
* @var PriceModifierInterface
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $priceModifier;
private $scopeConfig;

/**
* @param PriceCurrencyInterface $priceCurrency
* @param PriceModifierInterface $priceModifier
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
PriceCurrencyInterface $priceCurrency,
PriceModifierInterface $priceModifier
PriceModifierInterface $priceModifier,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
$this->priceModifier = $priceModifier;
$this->priceCurrency = $priceCurrency;
$this->scopeConfig = $scopeConfig;
}

/**
Expand All @@ -62,7 +63,10 @@ public function execute(
$product
);
// Apply catalog price rules to product options only if catalog price rules are applied to product.
if ($catalogRulePrice < $regularPrice) {
if (($catalogRulePrice < $regularPrice)
&& $this->scopeConfig->isSetFlag('catalog/catalog_price_rules/apply_to_custom_options',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE)) {

$optionPrice = $this->getOptionPriceWithoutPriceRule($optionPriceValue, $isPercent, $regularPrice);
$totalCatalogRulePrice = $this->priceModifier->modifyPrice(
$regularPrice + $optionPrice,
Expand Down
Loading