0

I use the following code to generate the coupon code programatically

$coupon = commerce_coupon_create('commerce_coupon_pct');
commerce_coupon_save($coupon);

If these code run in xxx.module file, it will write two coupons in the database when I refresh the page .

If these code writed in a theme template file like page.tpl.php file , it runs well .

Do someone know why ?

Thanks !

waveer
  • 1
  • 1
  • Probably, your code in .module file runs 2 times. In which place (what hook, submit handler) are you trying to create a coupon? – tastysoop Nov 21 '13 at 13:48
  • I just put these code in module file directly, no hook or submit handler, I am wrong ? Even if I put it in a custom named function like "generate_coupon", and I call the function in some hook like hook_init, it still generate two coupons in database. Is there some way I can debug which php code insert data to database ? Thank you very much for your reply! – waveer Nov 22 '13 at 02:24
  • Hi @tastysoop, I find new clue about my question, I describe it now, and maybe a little helpful for you to tell me more. I find it will generate two coupons only when I refresh a page, If i visit the page as the first time, it won't, and works well. Hope this helpful. – waveer Nov 22 '13 at 03:28

0 Answers0