I want to validate coupon code which is not in magento sales rules. I have millions of coupons in another table. I want to apply coupon from another table. If user enter any coupon first i want to look another table and search for coupon. if it exists then i will apply that coupon code on cart and show discount amount.

1 Answer 1


I'm not sure why using the builtin functionality for custom coupon code is not an option for you, since you could simply populate the table salesrule_coupon with your codes and assign that to the sales-rule you want and you're done. I would strongly recommend to use that functionality!

But anyway, if you really want to get the codes from somewhere else, I guess creating a Plugin would be the best solution for this. As far as I understand your requirement there are two possibilities - but be aware you have donwsides in both approaches. And in both cases you need a sales rule in Magento which applies a discount following the Magento concepts. It is only about applying a Magento rule for external hosted custom coupon codes:

  1. the custom coupon code is used only for searching for the "real" coupon code assiciated with the sales rule. After that the original code is used everywhere (in this case it will most likely also be shown to customer!)

  2. the custom coupon code is stored in the system and is always replaced internally with the "real" coupon code.

For achieving the first solution, you just need to implement a beforeExecute() function for the controller function Magento\Checkout\Controller\Cart\CouponPost::execute(), implement the identification of the "real" coupon code based on the input code there and if you find some match, replace the code.

For the second solution you may create a beforeLoad() function for the model Magento\SalesRule\Model\Coupon::load() function.

Exapmple for the second approach:


<type name="Magento\SalesRule\Model\Coupon">
    <plugin name="Vendor_Module_SalesRule_Coupon" type="Vendor\Module\Plugin\SalesRuleCoupon" sortOrder="10" disabled="false"  />


namespace Vendor\Module\Plugin;

class SalesRuleCoupon{

    protected $_logger;

    public function __construct(
        \Psr\Log\LoggerInterface $logger
            $this->_logger = $logger;

    public function beforeLoad(\Magento\SalesRule\Model\Coupon $coupon, $modelId, $field = null)
        //if model is loaded by code search for your matching data in your custom code
        if ($field == 'code'){
            $replacedCode = $this->getYourMatchingCode($modelId);
            if ($replacedCode !== null){
                $modelId = $replacedCode;
        return [$modelId, $field];

    private function getYourMatchingCode($originalCode)
        $replacedCode = null;
        //implement your logic here and set $replacedCode according to your match

        return $replacedCode;
  • I dont have anything in sales rule table. I have nearly 30 crore coupons in another table which i imported from another system (old website). I cant generate or import those coupons inside magento. I want that when i apply custom coupon in checkout somehow internally i will get discount price from my custom coupons table and apply it to cart.
    – Ashar Riaz
    Mar 13, 2019 at 16:12
  • could you please explain in detail your both methods.
    – Ashar Riaz
    Mar 13, 2019 at 16:13
  • I guess the answer is clear, I don't know what I should say more than that. If you are not familiar with plugíns at all, here is some documentation that may help you understand how plugins work -> devdocs.magento.com/guides/v2.2/extension-dev-guide/…
    – HelgeB
    Mar 13, 2019 at 16:36
  • Regarding the usage of cart rules you find the documentation here -> docs.magento.com/m2/ce/user_guide/marketing/… Here you can see how more custom coupon codes can be generated for one rule. That is what you should use (custom coupon codes can also be added from an external source using SQL for example)
    – HelgeB
    Mar 13, 2019 at 16:40
  • If you really want to have a complete independent functionality for applying discount, which has no relation to Magento's sales rule concept, you will have to implement your own module for that.
    – HelgeB
    Mar 13, 2019 at 16:59

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.