I am using magento 2.4.3 EE version

Is their any way to override below method

    protected function _calculate($rule, $item, $qty, $rulePercent)

for below classes

\Magento\SalesRule\Model\Rule::TO_PERCENT_ACTION =>
\Magento\SalesRule\Model\Rule::BY_PERCENT_ACTION =>
\Magento\SalesRule\Model\Rule::TO_FIXED_ACTION => \Magento\SalesRule\Model\Rule\Action\Discount\ToFixed::class,
\Magento\SalesRule\Model\Rule::BY_FIXED_ACTION => \Magento\SalesRule\Model\Rule\Action\Discount\ByFixed::class,
\Magento\SalesRule\Model\Rule::CART_FIXED_ACTION =>
\Magento\SalesRule\Model\Rule::BUY_X_GET_Y_ACTION =>

The _calculate method is common method for the above classes. I want to add some code in all these class to make changes in all cart rules.

Is there anyway to do within module ? I know about extending class & use of plugins so some idea needed on modifying this method for all classes at once.

  • any thoughts on this ?
    – devhs
    Sep 23, 2021 at 11:50
  • In the future, you might consider adding the specific M2 version tag to your question. Saves time if we know exactly where to look.
    – kookaburra
    Sep 23, 2021 at 11:56
  • 1
    updated tag in question.
    – devhs
    Sep 23, 2021 at 12:09

1 Answer 1


You might just need to take a second look at these classes. From my review of them, only ByPercent contains a protected _calculate($rule, $item, $qty, $rulePercent) method that would require a class extension. ToPercent extends from ByPercent, so overriding that method should resolve both.

As for the other classes, they appear only to have public calculate($rule, $item, $qty) methods, so a single plugin should suffice if you need to modify it in all classes. If you're really only interested in the protected method, though, than a single class extension of ByPercent is all you need.

  • So, the discount is set from the calculate($rule, $item, $qty) method for these options in rule (Percent of product price discount,Fixed amount discount,Fixed amount discount for whole cart,Buy X get Y free (discount amount is Y)) right ?
    – devhs
    Sep 23, 2021 at 12:27
  • I would say yes. The public calculate($rule, $item, $qty) methods all seem to do the heavy lifting for discount calculation in ToFixed, ByFixed, CartFixed, and BuyXGetY. The protected _calculate($rule, $item, $qty, $rulePercent) method does the heavy lifting in ByPercent, and in ToPercent because the latter extends from the former.
    – kookaburra
    Sep 23, 2021 at 14:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.