---
title: "How to Create Coupon Codes Programmatically in Magento 2"
url: "https://magecomp.com/blog/how-to-create-coupon-codes-programmatically-in-magento-2/"
date: "2022-10-24T10:00:41+00:00"
modified: "2025-02-13T11:53:31+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 348
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "Increase store sales by offering coupon codes at checkout. Learn How to Create Coupon Codes Programmatically in Magento 2. "
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Difference Between: Magento 2 WhatsApp Order Notification vs. Magento 2 SMS Notification"
    url: "https://magecomp.com/blog/difference-between-magento-2-whatsapp-order-notification-vs-magento-2-sms-notification/"
  - title: "Magento 2: Add Admin User name and Action name in Order Comment Section through Action Performed from Sales Order Grid"
    url: "https://magecomp.com/blog/magento-2-add-admin-user-name-and-action-name-in-order-comment/"
  - title: "How to call CMS Block content into Block File in Magento 2?"
    url: "https://magecomp.com/blog/call-cms-block-content-block-file-magento-2/"
---

# How to Create Coupon Codes Programmatically in Magento 2

_Published: October 24, 2022_  
_Author: Dhiren Vasoya_  

![](https://magecomp.com/blog/wp-content/uploads/2022/10/How-to-Create-Coupon-Codes-Programmatically-in-Magento-2-1024x512.webp)

**Hello Magento Friends,**

This blog will help you to learn How to Create Coupon Codes Programmatically in Magento 2.
[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-2.webp)](https://magecomp.com/services/hyva-theme-development/)

Magento 2 store owners can encourage customers to make a purchase and increase sales with the help of coupon codes. Coupon codes are created when you want to offer discounts to customers. [**Coupon Code Manager**](https://magecomp.com/magento-2-easy-coupon-manager.html) helps to effortlessly manage coupon codes of your Magento 2 store.
Magento 2 provides the option the [**create coupon codes through admin panel**](https://magecomp.com/blog/create-coupon-codes-in-magento-2/). However, you can create coupon codes programmatically in Magento 2.
## Steps to Create Coupon Codes Programmatically in Magento 2:
**Step 1:** You need to create a root script file on your Magento root directory.
Path of file: **Root Directory\pub\Customcoupon.php**

Now add the below code
```
<?php
use MagentoFrameworkAppBootstrap;

require __DIR__ . '/app/bootstrap.php';

$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('adminhtml');

$coupon['name'] = 'custom_coupon';
$coupon['desc'] = ' 10% Off Discount coupon.';
$coupon['start'] = date('Y-m-d');
$coupon['end'] = '';
$coupon['max_redemptions'] = 1;
$coupon['discount_type'] ='by_percent';
$coupon['discount_amount'] = 10;
$coupon['flag_is_free_shipping'] = 'no';
$coupon['redemptions'] = 1;
$coupon['code'] ='10%OFF';
$shoppingCartPriceRule = $obj->create('MagentoSalesRuleModelRule');
$shoppingCartPriceRule->setName($coupon['name'])
        ->setDescription($coupon['desc'])
        ->setFromDate($coupon['start'])
        ->setToDate($coupon['end'])
        ->setUsesPerCustomer($coupon['max_redemptions'])
        ->setCustomerGroupIds(array('0','1','2','3',))
        ->setIsActive(1)
        ->setSimpleAction($coupon['discount_type'])
        ->setDiscountAmount($coupon['discount_amount'])
        ->setDiscountQty(1)
        ->setApplyToShipping($coupon['flag_is_free_shipping'])
        ->setTimesUsed($coupon['redemptions'])
        ->setWebsiteIds(array('1'))
        ->setCouponType(2)
        ->setCouponCode($coupon['code'])
        ->setUsesPerCoupon(NULL);
$shoppingCartPriceRule->save();
```

Now, check the coupon code from the Magento 2 admin panel.
![coupon code](https://magecomp.com/blog/wp-content/uploads/2022/10/coupon-code.png)

## [![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-3.webp)](https://magecomp.com/services/hire-magento-programmer/)

## Conclusion:
This way, you can easily create coupon codes programmatically in Magento 2. If you have any doubts, let me know through the comment section. Share the article with your friends and stay updated so that you do not miss out on our latest tutorial.
[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-1.webp)](https://magecomp.com/services/magento-upgrade-services/)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/how-to-create-coupon-codes-programmatically-in-magento-2/](https://magecomp.com/blog/how-to-create-coupon-codes-programmatically-in-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-08 22:34:18 UTC_  
