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.
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] = array( 'amount' => $product->commerce_price[LANGUAGE_NONE]['amount'], 'currency_code' => $product->commerce_price[LANGUAGE_NONE]['currency_code'], );
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]['value'] = commerce_coupon_generate_coupon_code($limit_length); $coupon->commerce_coupon_number_of_uses[LANGUAGE_NONE]['value'] = 1; $coupon->commerce_coupon_fixed_amount[LANGUAGE_NONE] = array( 'amount' => 2000, 'currency_code' => 'EUR', ); commerce_coupon_save($coupon);