'How to create a discount coupon programmaticaly in opencart 2.1.0.2

I have created a newsletter subscription pop-up using acumbamail and what i want is to programatically create a -10% discount coupon, which will last for 7 days, everytime someone clicks subscribe, then i will email the coupon code to the customer. I know that in opencart 2.1.0.2 you can go to marketing->coupons but i dont have the choice to create a coupon that lasts for 7 days from the day that the customer subscribed to the newsletter.



Solution 1:[1]

You have to create coupon code (coupon code must be unique) and you have to store in the [db_prefix]coupon table then send it via email to customer. Did you analyze coupon table structure?

Here is an example with description (DB_PREFIX = oc_), I used INSERT INTO SET instead of INSERT INTO () VALUES (), because it's compact

INSERT INTO oc_coupon
   SET `name` = "New coupon", -- coupon description
       `code` = "COUPON", -- unique coupon code
       `type` = "P", -- P means percentage, F means flat
       `discount` = 10, -- 10% discount
       `logged` = 1, -- 1 means customer must be logged in, 0 means coupon can be used as guest 
       `shipping` = 0, -- free shipping when coupon used
       `total` = 0, -- coupon valid from cart subtotal
       `date_start` = CURRENT_DATE(), -- today
       `date_end` = DATE_ADD(CURRENT_DATE(), INTERVAL 6 DAY), -- today +6 days
       `uses_total` = 1, -- how many times the coupon can be used
       `uses_customer` = 1, -- how many times the coupon can be used per customer
       `status` = 1, -- coupon status
       `date_added` = NOW()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Baracsi Róbert