Skip to content

Instantly share code, notes, and snippets.

@ChuckMac
Last active August 29, 2015 14:10
Show Gist options
  • Save ChuckMac/910f4250c26737bb4694 to your computer and use it in GitHub Desktop.
Save ChuckMac/910f4250c26737bb4694 to your computer and use it in GitHub Desktop.
Create WooCommerce Coupon Automagically
add_filter( 'woocommerce_coupon_code', 'create_coupon' );
function create_coupon ( $coupon ) {
if ( '' != $coupon ) {
//$the_coupon = new WC_Coupon( $coupon );
if ( ! $the_coupon->id ) {
$coupon_data = array(
'post_title' => $coupon,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
);
$coupon_id = wp_insert_post( $coupon_data );
update_post_meta( $coupon_id, 'discount_type', 'percent_product' );
update_post_meta( $coupon_id, 'coupon_amount', '50' );
}
}
return $coupon;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment