Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global $CPN;
- $coupon_code = $CPN['user']->ID.' '.date('d.m.Y h:i'); // Code - perhaps generate this from the user ID + the order ID
- $discount_type = 'fixed_cart'; // 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 );
- 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', 'yes' );
- 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', '1' );
- 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', 'yes' );
- $order = new WC_Order();
- $order->apply_coupon( $coupon_code );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement