Get Number of Times Coupon Code is Used in Magento

Get Number of Times Coupon Code is Used in Magento

September 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Get Number of Times Coupon Code is Used in Magento

Retrieving Coupon Code Usage Statistics in Magento

In Magento, the SalesRule module indeed stores information about applied coupon codes along with various metrics. To get the number of times each coupon code is used, you can query the database directly or use the provided API endpoints.

Direct Database Query Approach

The salesrule_coupon_usage table contains the usage details for each applied coupon code. You can write a simple SQL query to retrieve this information:

SELECT scu.code, COUNT(*) AS total_uses
FROM salesrule_coupon_usage AS scu
GROUP BY scu.code;

This query will return a list of coupon codes and the corresponding number of times they have been used.

Using Magento API Endpoints

For a more reliable and secure approach, you can use the built-in REST APIs provided by Magento. To retrieve the coupon code usage statistics:

  1. Enable the REST API in your Magento admin panel (System > Services > Configuration).
  2. Use the following endpoint to get all the applied coupon codes:
GET /rest/V1/couponlink?searchCriteria[filterGroups][0][filters][0][field]=applied&searchCriteria[filterGroups][0][filters][0][value]=1

This will return a list of used coupons with their usage details.

  1. To get the total number of uses for each code, iterate through the response and count the occurrences of each coupon:
$response = json_decode(file_get_contents($apiUrl), true);
$totalUsesByCode = [];

foreach ($response['items'] as $item) {
    if (isset($totalUsesByCode[$item['code']])) {
        $totalUsesByCode[$item['code']]++;
    } else {
        $totalUsesByCode[$item['code']] = 1;
    }
}

Best Practices

  • Always validate and sanitize the input data to prevent SQL injection attacks.
  • Use pagination when fetching large amounts of data from the database or API.
  • Consider implementing caching mechanisms for frequently accessed query results.

Remember that Magento’s built-in APIs are more secure than directly querying the database, as they provide an additional layer of application-level validation. However, if you need to optimize performance significantly, a direct SQL query might be more efficient.

Conclusion

Retrieving coupon code usage statistics in Magento is straightforward once you understand where the data is stored and how to access it. Whether you choose to use direct database queries or Magento’s built-in APIs depends on your specific needs and preferences.

For further reference on Magento development best practices, I recommend checking out our comprehensive guides at Laravel Company.

Database API Extensions Admin

Our network