Advertisement
Guest User

Untitled

a guest
Aug 18th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.05 KB | None | 0 0
  1. global $CPN;
  2.  
  3. $coupon_code = $CPN['user']->ID.' '.date('d.m.Y h:i'); // Code - perhaps generate this from the user ID + the order ID
  4. $discount_type = 'fixed_cart'; // fixed_cart, percent, fixed_product, percent_product
  5.  
  6. $coupon = array(
  7.     'post_title' => $coupon_code,
  8.     'post_content' => '',
  9.     'post_status' => 'publish',
  10.     'post_author' => 1,
  11.     'post_type'     => 'shop_coupon'
  12. );
  13.  
  14. $new_coupon_id = wp_insert_post( $coupon );
  15.  
  16. update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
  17. update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
  18. update_post_meta( $new_coupon_id, 'individual_use', 'yes' );
  19. update_post_meta( $new_coupon_id, 'product_ids', '' );
  20. update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
  21. update_post_meta( $new_coupon_id, 'usage_limit', '1' );
  22. update_post_meta( $new_coupon_id, 'expiry_date', '' );
  23. update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
  24. update_post_meta( $new_coupon_id, 'free_shipping', 'yes' );
  25.  
  26. $order = new WC_Order();
  27.  
  28. $order->apply_coupon( $coupon_code );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement