I'm using the Commerce Coupon Fixed Amount module to create fixed coupons, which works fine in the interface. But I'm wondering if there is a way to programmatically create them.
I'm able to create normal commerce coupons with this helpful snippet of code.
3 Answers
Using this tutorial and the above modules/code It makes making a commerce coupon fixed amount (or in this case, a gift card) easier to see. The only difference between making a regular coupon and one with a fixed amount is that the product (gift card product) price needs to be loaded before hand and the amount of the coupon being created upon gift card purchase needs to load the price from that gift card:
$coupon->commerce_coupon_fixed_amount[LANGUAGE_NONE][0] = array(
'amount' => $product->commerce_price[LANGUAGE_NONE][0]['amount'],
'currency_code' => $product->commerce_price[LANGUAGE_NONE][0]['currency_code'],
);
This isn't a way to do it programmatically, but you can use the Commerce Coupon Batch module, which quickly allows you to generate a large number of coupons through the UI, which you can then export to Excel, etc.
It integrates with the Coupon Fixed Amount module.
The code i used was :
$coupon = commerce_coupon_create($type);
$limit_length = variable_get('commerce_coupon_default_code_size', 8);
$coupon->commerce_coupon_code[LANGUAGE_NONE][0]['value'] = commerce_coupon_generate_coupon_code($limit_length);
$coupon->commerce_coupon_number_of_uses[LANGUAGE_NONE][0]['value'] = 1;
$coupon->commerce_coupon_fixed_amount[LANGUAGE_NONE][] = array(
'amount' => 2000,
'currency_code' => 'EUR',
);
commerce_coupon_save($coupon);