How to validate custom coupons in magento 2?

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.

Solutions

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:

di.xml

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

Vendor/Module/Plugin/SalesRuleCoupon.php

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;
    }
}

Similar questions

Magento Coupons limit per use o per customer not working
In the screenshot you can see my options. I have created a coupon to have the free first shippings. What i need is to have one unique code (Example: FREEFIRSTSHIPPING) that can be used only one time, but that every logged customers can use this same code one time. The code works well, setting my Uses per Coupon value to 0, but i have noticed that i...
Magento apply discount coupons
I want to apply coupon code in magento landing page , i just saw the code, but according to code, the code apply on cart's amount and after deducting coupon code, it apply the price on cart, but i want to apply code on a specific static price, can it be possible? if yes, then please help me to solve it out. Thanks
magento 1.7.x remove Sales Rules & Coupons for using database
I have direct database of magento 1.7.x and I have to remove all Sales Rules & Coupons using mysql Query. I don't want to use any magento code even PHP code. I have just need database query to remove all my Coupons form database.
Magento 2 Data migration for only customers, categories and coupons
I want to migrate data(only customers, categories and coupons) from magento 1.9.0.1(CE) to magento 2.1.0(CE) I am following Steps from - http://devdocs.magento.com/guides/v2.0/migration/migration-tool-install.html for data migration. What I understood is in 1. m2project/vendor/magento/data-migration-tool/etc/ce-to-ce/1.9.0.1/config.xml I defined so...
Working of discount coupons on the checkout page in Magento 2.2.2
I am trying to understand the working of discount coupon codes on the checkout page. When we apply any coupon code, then the discount is added to cart without reloading the page. I have checked and found some below points: When we apply any coupon code then, using vendor\magento\module-sales-rule\view\frontend\web\js\action\set-coupon-code.js, an a...
How to customise the coupons in Magento 2?
I want to apply 50% coupon to lowest product only. If the cart has 2 products means when the customer applies the coupon "COUPON50". Product A = $20 Product B = $10 then the coupons must be apply to product B i.e., $5 discount only. We did googled but no clue to get going. Am pretty new to this concepts, any help would be appreciable. thanks

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.