Title: Generating coupon codes programatically
Last modified: August 31, 2016

---

# Generating coupon codes programatically

 *  Resolved [beamkiller](https://wordpress.org/support/users/beamkiller/)
 * (@beamkiller)
 * [10 years ago](https://wordpress.org/support/topic/generating-coupon-codes-programatically/)
 * Hi,
 * I have succesfully write a small snippet to generate coupon codes from an array.
   Everything works except, the title of the coupon codes.
 * My [snippet](https://snag.gy/ApQa7O.jpg), which is from [WooCommerce ](https://docs.woothemes.com/document/create-a-coupon-programatically/)
   pages.
 * So this will generate two coupons, but the title has no leading zeros as my array
   elements. See [this](https://snag.gy/l01Lxz.jpg).
 * I have also tried woocommerce_coupon_code hook.
 *     ```
       /*-----------------------------------------------------------------------------------*/
       /* Add Leading Zeros before coupon codes generating */
       /*-----------------------------------------------------------------------------------*/
       function zerofill ($num, $zerofill = 8)
       {
       	return str_pad($num, $zerofill, '0', STR_PAD_LEFT);
       }
   
       // add the filter 
       add_filter( 'woocommerce_coupon_code', 'filter_woocommerce_coupon_code', 10, 1 );
   
       // define the woocommerce_coupon_code callback 
       function filter_woocommerce_coupon_code( $post_post_title )
       {
       	//error_log("COUPON: ".$post_post_title);
       	return zerofill($post_post_title);
       }
       ```
   
 * [https://wordpress.org/plugins/woocommerce/](https://wordpress.org/plugins/woocommerce/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Contributor [Mike Jolley](https://wordpress.org/support/users/mikejolley/)
 * (@mikejolley)
 * [10 years ago](https://wordpress.org/support/topic/generating-coupon-codes-programatically/#post-7435704)
 * I think it’s because you’re using integers and not strings. Integers cannot have
   leading zero.
 *  Thread Starter [beamkiller](https://wordpress.org/support/users/beamkiller/)
 * (@beamkiller)
 * [10 years ago](https://wordpress.org/support/topic/generating-coupon-codes-programatically/#post-7435715)
 * Ah, you were right!
 * Thanks Mike! 🙂

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Generating coupon codes programatically’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [hook](https://wordpress.org/support/topic-tag/hook/)

 * 2 replies
 * 2 participants
 * Last reply from: [beamkiller](https://wordpress.org/support/users/beamkiller/)
 * Last activity: [10 years ago](https://wordpress.org/support/topic/generating-coupon-codes-programatically/#post-7435715)
 * Status: resolved