I want to import 20.000 coupon codes on my website, and for this reason, I created the following script:

use Magento\Framework\App\Bootstrap; 

 * If your external file is in root folder
require __DIR__ . '/app/bootstrap.php';

// Import CSV from ViArt format:
$handle = fopen('coupons.csv', 'r');
$cols   = array_flip(fgetcsv($handle));

while($data = fgetcsv($handle))

    if($data[$cols['is_active']] == 1)
        echo 'Importing coupon with code: '.$data[$cols['coupon_code']].'<br />';
    } else {
        echo 'Not imported (not active): '.$data[$cols['coupon_code']].'<br />';

function createCoupon($code, $description, $type, $amount, $options = array())

$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');

$coupon['name'] = '10% Footers';
$coupon['desc'] = 'Discount for vip signup coupon.';
$coupon['start'] = date('Y-m-d');
$coupon['end'] = '';
$coupon['max_redemptions'] = 1000;
$coupon['discount_type'] ='by_percent';
$coupon['discount_amount'] = 15;
$coupon['flag_is_free_shipping'] = 'no';
$coupon['redemptions'] = 1;
$coupon['code'] ='NL01-1234'; //this code will normally be autogenetated but i am hard coding for testing purposes

$shoppingCartPriceRule = $obj->create('Magento\SalesRule\Model\Rule');


At first, the script only imported 100 coupons, but I saw error_log and discovered that the problem was memory_limit of php.ini. I incremented this memory limit and the script can import 170 coupons now.

How can i do to import the 20.000 coupons?

Thank you very much!

  • If you're ready to purchase then you can purchase this extension Import coupon codes which will allow us to generate and import multiple codes per cart price rule. You can also check out other features as well
    – Mohit Rane
    Jun 2, 2021 at 10:13

2 Answers 2

  1. Get csv data in batch -> solution
  2. What is the current memory limit though? :C I can hardly believe that you can process no more than 170 items...

You can do this using MYSQL workbench if you don't want to write code

Create your rule and codes in magento admin Navigate to salesrule_coupon and export it to csv using the table data export wizard, which you can access by right clicking the table in the left pane In MYSQL workbench run sql similar to DELETE FROM magento.salesrule_coupon WHERE rule_id = id of rule you created; Remove all codes from the csv that are not for the rule_id you created in excel or similar app Paste your new codes over the top of the auto generated ones in the csv Remove the created_at column if you have one as this can make import error Right click the table again but this time choose table data IMPORT wizard Choose your csv, this may cause the dialog to close and you will have to run the wizard again Complete the wizard and your rule will now have your custom codes instead of the ones Magento auto generated

  • This is really hard to read. Please make use of line breaks and punctuation to make it easier to understand. Nov 29, 2023 at 15:49

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.