1

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'))); 
  $model->setCouponType(2);
  $model->setCouponCode($couponCode);
  $model->setUsesPerCoupon(1);
  $model->setUsesPerCustomer(1);
  $model->setCustomerGroupIds($customerGroupIds);
  $model->setIsActive(1);
  $model->setConditionsSerialized('');
  $model->setActionsSerialized('');
  $model->setStopRulesProcessing(1);
  $model->setIsAdvanced(1);
  $model->setProductIds('');
  $model->setSortOrder(1);
  $model->setSimpleAction('by_percentage');//$model->setSimpleAction('by_fixed');
  $model->setDiscountAmount(30);
  $model->setDiscountStep(0);
  $model->setSimpleFreeShipping(0);
  $model->setTimesUsed(0);
  $model->setIsRss(0);
  $model->setWebsiteIds($websitesId);

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

  try {
      $model->save();
      //return $model->getData();
      echo json_encode( Array(
          "code" => $couponCode
        )
      );
  } catch (Exception $e) {
      print_r($e->getMessage());exit;
      Mage::log($e->getMessage());
  }

}

1 Answer 1

2

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.