2

I've been looking for a way to add coupons in bulk to WooCommerce, it's actually a list of 800 membership numbers, that grant a discount, and coupons seem to the best way to do this.

I've found a way to add a single coupon programatically: http://docs.woothemes.com/document/create-a-coupon-programatically/ but my limited PHP knowledge doesn't seem adequate to add 800.

I'm guessing an array or somehow linking to a .csv would do it, but I'm not sure. I'd be grateful for any help.

friendly_llama
  • 349
  • 1
  • 5
  • 15
  • Do you know about this product [Smart Coupons](http://www.woothemes.com/products/smart-coupons/). With this product you can create many unique coupon code, can import all those coupon in WooCommerce with few click, can export those generated coupon code in CSV file. This product provides many functionality for WooCommerce Coupons. In product's documentation you'll not find content for auto generation & import because product documentation is not updated for long time. – Ratnakar - StoreApps Jul 23 '13 at 05:21
  • Thanks for the suggestion, it does look feasible, but I'm cautious of adding such a vast array of features and another plugin, as so much more complexity for one required feature isn't ideal. – friendly_llama Jul 23 '13 at 09:48

1 Answers1

3

You could create an array of 800 diferent keys and just make a foreach loop to repeat the process for each different key you have on the array like this

forehach ( $your_800_array as $coupon_code){
 //the code from the page you posted
 $amount = '10'; // Amount
 $discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
 $coupon = array(
 'post_title' => $coupon_code,
 'post_content' => '',
 'post_status' => 'publish',
 'post_author' => 1,
 'post_type' => 'shop_coupon'
 );
 $new_coupon_id = wp_insert_post( $coupon );
 // Add meta
 update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
 update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
 update_post_meta( $new_coupon_id, 'individual_use', 'no' );
 update_post_meta( $new_coupon_id, 'product_ids', '' );
 update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
 update_post_meta( $new_coupon_id, 'usage_limit', '' );
 update_post_meta( $new_coupon_id, 'expiry_date', '' );
 update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
 update_post_meta( $new_coupon_id, 'free_shipping', 'no' );
}

(Used the code that OP posted inside the loop. Not actually tested)

Jorge Rivera
  • 161
  • 1
  • 6