🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How to Get All Coupon Code of Particular Cart Price Rules in Magento 2

By Jignesh ParmarUpdated on May 21, 2025 2 min read

Providing attractive discounts on weekends, festivals, first-order, etc., is one of the best tactics to boost sales quickly for the Magento 2 stores. The store owner can create coupon codes for offering discounts and promote them on various advertising mediums.

Coupon codes are generated and applied with Cart Price Rules – which allows applying discounts in the shopping cart based on a set of conditions and coupon codes. Discounts are applicable only when a customer enters a valid coupon code on the shopping cart page.

A store owner can easily create coupon codes as well as multiple ones but what if the requirement arises to get all coupon code of particular cart price rules in Magento 2?

For instance, an admin needs to get the list of coupon codes in the grid or CSV. Additionally, one may also need to provide all coupon codes to a marketing team for advertising purposes!

Also Read: How to Auto Apply Coupon Code in Magento 2

In such a scenario, use the below code and get all coupon codes of particular cart price rules and print according to the business requirement.

Method to Get All Coupon Code of Particular Cart Price Rules in Magento 2

Use the below code in your php file.

<?php

ini_set('memory_limit', -1);

use Magento\Framework\App\Bootstrap;

require 'app/bootstrap.php';

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

$om = \Magento\Framework\App\ObjectManager::getInstance();
$couponFactory = $om->get('Magento\SalesRule\Model\CouponFactory');
$ruleId = 2296;
$p = 0;

while (1) {
    $p++;
    $couponCodeData = $couponFactory->create()
        ->getCollection()
        ->addFieldToFilter('rule_id', $ruleId)
        ->setPageSize(20)
        ->setCurPage($p);

    foreach ($couponCodeData as $item) {
        echo $item->getcode() . '</br>';
    }

    if ($p >= $couponCodeData->getLastPageNumber()) {
        break;
    }
}

That’s it.

I would be happy to answer.

Do consider sharing this post with Magento Community via social media.

Thank you.

Jignesh Parmar Full Image
Article byJignesh Parmar

An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.