magento get coupon details from price rule

magento get coupon details from price rule

October 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento get coupon details from price rule

Retrieving Coupon Details from Magento Rule ID

As an experienced e-commerce developer, I regularly assist clients seeking ways to extract coupon details from their Magento rule IDs. In this post, we’ll explore how to achieve this using the Magento framework and its built-in resources.

Understanding Magento’s Rule Collection

First, let’s review your existing code for retrieving active shopping cart rules:

$sopping_cart_rules = Mage::getResourceModel('salesrule/rule_collection')->load();
$sopping_cart_rule_info = array();
foreach ($sopping_cart_rules as $rule) {
    if ($rule->getIsActive()) {
        $sopping_cart_rule_info['shopping_cart_rules'] = array(
            'id' => $rule->getId(),
            'name' => $rule->getName()
        );
    }
}

This snippet successfully loads all active shopping cart rules and extracts the rule ID and name. However, it doesn’t provide a way to directly access coupon codes associated with each rule.

Accessing Coupon Codes from a Rule ID

To retrieve coupon details for a specific rule, we need to query the salesrule_coupon table using the provided rule ID. Here’s an updated approach:

$ruleId = 123; // Replace with your desired rule ID
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');

$sql = "SELECT `code`, `description`
        FROM {$resource->getTableName('salesrule_coupon')}
        WHERE `rule_id` = :ruleId";
$bindParams = array(':ruleId' => (int)$ruleId);

$coupons = $readConnection->fetchAll($sql, $bindParams);

$sopping_cart_rule_info['shopping_cart_rules'] = array(
    'id' => $ruleId,
    'name' => Mage::getModel('salesrule/rule')->load($ruleId)->getName(),
    'coupons' => $coupons
);

In this code:

  1. We initialize the rule ID and retrieve a read-only database connection.
  2. We construct an SQL query to fetch the code and description columns from the salesrule_coupon table for the specified rule ID.
  3. We execute the query using the fetched connection, passing the rule ID as a parameter to prevent SQL injection attacks.
  4. The resulting coupons are stored in the $coupons array alongside the rule information.

Conclusion

Retrieving coupon details from a Magento rule ID involves querying the salesrule_coupon table using the provided rule ID. By following the approach demonstrated above, you can efficiently extract relevant coupon data for your shopping cart rules.

Remember to replace the placeholder values with your actual rule IDs and adjust the code as needed to fit your specific project requirements.

If you’re looking to further enhance your e-commerce development skills or explore more advanced Magento topics, I recommend visiting Laravel Company. They offer a wealth of resources and expert guidance for developers working with popular frameworks like Laravel and Magento.

Checkout Database Pricing CLI

Our network