Is your promo active? Magento 1

posted by 4 years ago and updated 4 years ago

There is one thing Magento telling you that a sales rule is active and there is another that rule being truly active.

Magento 1 uses has the active data attached to a sales rule $rule->getIsActive(). That tells us whether the specified promotion is activated by the user. What it won't tell us is whether we can use that promotion based on date availability.

We wanted to help our customer run a campaign over Christmas until New Years. It was easy to set it up. What was not clear was showing a promotional banner message at the top of the website while the promotion was running.

We wanted to show a promotion for our sales rule. If we just checked whether the promo was active. The promotion message would still be showing.

The solution comes from checking 3 things not one.

  • Is the rule active
  • Is the current date after the FromDate
  • Is the current date before the ToDate
// Load a promotion and check whether it is active and it is within a valid time range        
function isPromoActive($rule_id){
    $rule = Mage::getModel('salesrule/rule')->load($rule_id);

    $now = date('Y-m-d');
    // Set a now date if there is no date set as a failsafe
    $from_date = $rule->getFromDate() ?: $now;
    $to_date = $rule->getToDate() ?: $now;

    // if date is with in range of now
    return ($rule->getIsActive() && (($now >= $from_date) && ($now <= $to_date)) );
    
}

// Use like so
if(isPromoActive(14)) { 
    // do something 
}

Now we were happy and our customer was happy. We could go on holiday while the promotion message would go to sleep once the promotion expired.

Tags:

Do you need help? Sometimes it is just easier to ask

In case you need that little extra push with your project. We are always happy to collaborate with new people in the industry.

Contact us for help