I'm creating a coupon code programatically and it works just fine. It makes the coupon code, and the coupon works. It shows up in the back end of magento and is ok. If I go the checkout page, it shows the discount is applied.

However, if I go to the shopping cart page, the coupon will work, but it does not display in the totals.

If I go to the back end of magento, find that coupon code and hit save, then the discount shows up on the shopping cart page. Any idea what's happening?

Here's the bulk of the code:

  public function generatecodeAction(){

  //generate coupon code
  $websitesId = Mage::getModel('core/website')->getCollection()->getAllIds();
  $couponCode = Mage::helper('core')->getRandomString(9);
  $customerGroupIds = Mage::getModel('customer/group')->getCollection()->getAllIds();

  $model = Mage::getModel('salesrule/rule');
  $model->setName('Discount for Hover');
  $model->setDescription('Discount for Hover');
  $model->setFromDate(date('Y-m-d H:i:s'));
  $model->setToDate(date('Y-m-d H:i:s', strtotime('+10 minutes'))); 

  $labels = array();
  $labels[0] = 'Default store label';//default store label
  $labels[1] = 'Website id store label';

  try {
      //return $model->getData();
      echo json_encode( Array(
          "code" => $couponCode
  } catch (Exception $e) {


1 Answer 1


This kinda works, but it's wrong. $model->setSimpleAction('by_percentage');

It should be $model->setSimpleAction('by_percent');

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.