Skip to content

Instantly share code, notes, and snippets.

@amanaplan
Created August 2, 2016 20:08
Show Gist options
  • Save amanaplan/5bda5861833a8676d2781a487c81cdd0 to your computer and use it in GitHub Desktop.
Save amanaplan/5bda5861833a8676d2781a487c81cdd0 to your computer and use it in GitHub Desktop.
Create a single-use Commerce coupon programmatically
<?php
/**
* Create a single-use Commerce coupon programmatically
*/
// Get all available coupon types
// $types = commerce_coupon_get_types();
// Create a default coupon instance
$coupon = commerce_coupon_create('example_coupon_type');
// Generate an unique, six-character "coupon code"
$coupon_code = strtoupper(commerce_coupon_generate_coupon_code(6));
// Set the coupon's values and conditions
$coupon->commerce_coupon_code[LANGUAGE_NONE][0]['value'] = $coupon_code;
$coupon->commerce_discount_reference[LANGUAGE_NONE][0]['target_id'] = 4;
$coupon->commerce_coupon_conditions[LANGUAGE_NONE][0] = array(
'condition_name' => 'commerce_coupon_usage_evaluate_usage',
'condition_settings' => array(
'max_usage' => 1
)
);
// Save the new coupon
commerce_coupon_save($coupon);
// Check Coupon
$coupon = commerce_coupon_load_by_code($coupon_code);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment