Is there someway to find all the coupon codes which is applicable for the logged in customer?

One way is to collect all the coupon codes, loop over them and apply to quote object in order to check it's validity. Something similar to following code:

$eligibleCoupons = array();
$allAvailableCoupons = array('xxx', 'yyy', 'zzz', /* ... */);
foreach ($allAvailableCoupons as $couponCode) {
    $codeLength = strlen($couponCode);
    $isCodeLengthValid = $codeLength && $codeLength <= Mage_Checkout_Helper_Cart::COUPON_CODE_MAX_LENGTH;

    $this->_getQuote()->setCouponCode($isCodeLengthValid ? $couponCode : '')

    if ($codeLength) {
        if ($isCodeLengthValid && $couponCode == $this->_getQuote()->getCouponCode()) {
            $eligibleCoupons[] = $couponCode
            //finally remove coupon code apply
        } else {


But this method seems lengthy and resource consuming.

I bet there must be an easier approach for this.

  • Is there any restrictions on shopping cart rules? If so, you can simplify checking conditions and make everything in the sql.
    – Amasty
    Commented Aug 6, 2014 at 21:16
  • Yes there are different coupon codes applicable for different conditions. Not sure if using only pure SQL is enough.
    – MagePsycho
    Commented Aug 7, 2014 at 5:32

1 Answer 1


The coupon codes and shopping cart rules in general (even if they don't have a coupon code) have meaning relative to a cart (quote) object. They ca depend on different qtys in the cart, they can depend on shipping and billing addresses, or even on shipping and payment methods.
So you have to involve a quote in the process of validating a rule.
Magento has something for getting the valid rules depending on a quote and a coupon code.
See Mage_SalesRule_Model_Validator::init.
Inside that method there is this code that (I think) it filters the valid rules depending on the coupon code, website id and customer group.

$this->_rules[$key] = Mage::getResourceModel('salesrule/rule_collection')
            ->setValidationFilter($websiteId, $customerGroupId, $couponCode)

The code that actually retrieves these rules is located in Mage_SalesRule_Model_Resource_Rule_Collection::setValidationFilter.
This method receives 4 parameters.

  • the website id - You already have that one
  • the customer group - you already have that one
  • the coupon code - this is what you need.
  • a date that defaults to now - you have that one.

So my idea is to build your own method that is similar to the setValidationFilter but remove from that the conditions for the coupon codes.
You will end up with a set of rules that can be valid if you have the proper coupon code.
Then just loop through the rules you get and check which ones have coupon codes.
Those are the coupon codes you are looking for.
Fogot one thing. The method I mentioned does not involve a quote. It just filters a bit the rules. You will still have to apply the coupons to a quote and see if they are valid, but at least you eliminate some of the rules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.