Get Customer Name for Coupon Codes in Magento

Get Customer Name for Coupon Codes in Magento

September 5, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Get Customer Name for Coupon Codes in Magento

Magento: Retrieving Customer Name for Coupon Codes

Daniel, I understand your frustration in trying to retrieve the customer name associated with a coupon code in Magento. The default Magento models don’t provide this information directly, but we can achieve it through some custom queries.

The key lies in understanding how Magento tracks coupon usage and its relationship with sales orders. When a customer applies a coupon during checkout, a new row is inserted into the salesrule_coupon_to_order table for each order that used the coupon.

Let’s walk through this step-by-step.

Step 1: Retrieve Coupon Usage Information

Your current approach of loading the coupon model (Mage::getModel('salesrule/coupon/usage')) is correct. It allows you to retrieve information about how many times a specific coupon code has been used across all orders.

The relevant fields in this context are times_used (number of times the coupon was applied) and customer_id (the ID of the customer who last used the coupon).

Your provided PHP code correctly loads the coupon model and retrieves these two values. However, the getCustomerId() method only returns the ID of the customer who last applied the coupon.

To get the full name of this customer, we need to use the Magento Customer API.

Step 2: Retrieving Customer Data

Magento provides a customer resource model (Mage_Customer_Model_Resource_Abstract) that can be used to fetch customer data. We can use it in conjunction with the customer ID retrieved from the coupon usage information.

Here’s an updated version of your code:

<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;

umask(0);
Mage::app('admin');

// Load the coupon model and retrieve its details
$coupon = Mage::getModel('salesrule/coupon');
$coupon->load('dd_38WX9N', 'code');

if ($coupon->getId()) {
    $timesUsed = $coupon->getTimesUsed();
    $customerId = $coupon->getCustomerId();

    // Load the customer model using the retrieved ID
    $customerModel = Mage::getModel('customer/customer');
    $customer = $customerModel->load($customerId);

    echo "Coupon Code: " . $coupon->getCode() . "\n";
    echo "Times Used: " . $timesUsed . "\n";
    echo "Customer Name: " . $customer->getName();
}
?>

In this code, we first load the customer model using the Mage::getModel('customer/customer') method. We then use the load() function to retrieve the customer data based on the ID retrieved from the coupon.

The getName() function returns the full name of the customer associated with the coupon.

Remember that this code should be placed in a Magento template file or a custom module controller, as loading the Mage.php file directly is not recommended for production environments due to security reasons.

Conclusion

Retrieving the customer name associated with a coupon code requires navigating through the relationship between salesrule_coupon_to_order and customer_entity tables. By using the Magento resource models correctly, we can easily retrieve this information.

If you’re looking to build more complex customizations like this in your Magento store, I recommend checking out our guide on Magento Customization Best Practices for a deeper understanding of the Magento architecture and how to safely modify its core functionality.

Remember to always test changes thoroughly before deploying them to a live production environment. Let me know if you have any other questions!

Happy coding, [Your Name] Senior Developer, Laravel Company

Checkout Database API Orders

Our network