0

I am looking for a solution to get a custom Action > Apply in Catalog Price Rules custom to increase the price of specific products instead of just giving a discount

As a test, we tried adjusting the calculation below but after indexing it does not seem to do anything

  • /vendor/magento/module-catalog-rule/Helper/Data.php code:
<?php

namespace Magento\CatalogRule\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * Algorithm for calculating price by rule
     *
     * @param  string $actionOperator
     * @param  int $ruleAmount
     * @param  float $price
     * @return float|int
     */
    public function calcPriceRule($actionOperator, $ruleAmount, $price)
    {
        $priceRule = 0;
        switch ($actionOperator) {
            case 'to_fixed':
                $priceRule = min($ruleAmount, $price);
                break;
            case 'to_percent':
                $priceRule = $price * $ruleAmount / 100;
                break;
            case 'by_fixed':
                $priceRule = max(0, $price - $ruleAmount);
                break;
            case 'by_percent':
                $priceRule = $price * (1 - $ruleAmount / 100);
                break;
        }
        return $priceRule;
    }
}
  • /vendor/magento/module-catalog-rule/Model/Indexer/ProductPriceCalculator.php code:
<?php

namespace Magento\CatalogRule\Model\Indexer;

class ProductPriceCalculator
{
    /**
     * @var \Magento\Framework\Pricing\PriceCurrencyInterface
     */
    private $priceCurrency;

    /**
     * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
     */
    public function __construct(\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency)
    {
        $this->priceCurrency = $priceCurrency;
    }

    /**
     * Calculates product price.
     *
     * @param array $ruleData
     * @param null $productData
     * @return float
     */
    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); 
    }
}

And I am only looking for a solution within the Catalog Price Rules.

Any help would be very welcome.

enter image description here

3
  • This is tricky because Magento makes the broad assumption that the lowest calculated price is always appropriate. To be fair, between prices, special prices, tier prices, customer group prices and price rules, it would be hard to support any alternative. Take a look at the observers \Magento\CatalogRule\Observer\ProcessFrontFinalPriceObserver and \Magento\CatalogRule\Observer\ProcessAdminFinalPriceObserver for another point where your custom calculation may be overridden when min() is applied.
    – jiheison
    Commented Nov 16, 2021 at 17:04
  • Thank you for your response, I will look at those files. We now have the idea that Magento somewhere seems to compare the new price with the current price and if it is higher it does not apply the rule. We have this feeling but no idea if this is true and we cannot find it..
    – Timo
    Commented Nov 17, 2021 at 11:17
  • Checking the records in catalogrule_product_price and catalog_product_index_price might tell you more about what prices are actually being enacted, and whether it is directly after a change or only after a full reindex.
    – jiheison
    Commented Nov 17, 2021 at 17:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.