How to calculate product price based on custom action in Catalog Price Rules
-
07-02-2021 - |
Question
I have added new action in catalog price rule.
I have added this action through overriding below file.
module-catalog-rule/Model/Rule/Action/SimpleActionOptionsProvider.php
When this action is applied then how to calculate product price based on this action.
In magento module-catalog-rule which file/function is used to calculate product price when other action is applied.
Anyone knows then please help.
Solution
I have been recently fighting with something similar. Take a look at \Magento\CatalogRule\Model\Indexer\ProductPriceCalculator class
public function calculate($ruleData, $productData = null)
{
if ($productData !== null && isset($productData['rule_price'])) {
$productPrice = $productData['rule_price'];
} else {
$productPrice = $ruleData['default_price'];
}
switch ($ruleData['action_operator']) {
case 'to_fixed':
$productPrice = min($ruleData['action_amount'], $productPrice);
break;
case 'to_percent':
$productPrice = $productPrice * $ruleData['action_amount'] / 100;
break;
case 'by_fixed':
$productPrice = max(0, $productPrice - $ruleData['action_amount']);
break;
case 'by_percent':
$productPrice = $productPrice * (1 - $ruleData['action_amount'] / 100);
break;
default:
$productPrice = 0;
}
return $this->priceCurrency->round($productPrice);
}
You'd validate your custom value is being received in $ruleData['action_operator'] and then add a new case with your custom logic
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange