How do I programmatically apply a coupon in Woocommerce for first order made by a customer?


In Woocommerce I'm trying to find a way to apply a 20% discount to the first order made by a new customer. It appears that I can use the functions woocommerce_after_checkout_validation and check_new_customer_coupon to do this but it doesn't work.


add_action('woocommerce_after_checkout_validation','check_new_customer_coupon', 0); function check_new_customer_coupon(){ global $woocommerce; // you might change the name of your coupon $new_cust_coupon_code = 'test'; $has_apply_coupon = false; foreach ( WC()->cart->get_coupons() as $code => $coupon ) { if($code == $new_cust_coupon_code) { $has_apply_coupon = true; } } if($has_apply_coupon) { if(is_user_logged_in()) { $user_id = get_current_user_id(); // retrieve all orders $customer_orders = get_posts( array( 'meta_key' => '_customer_user', 'meta_value' => $user_id, 'post_type' => 'shop_order', 'numberposts'=> -1 ) ); if(count($customer_orders) > 0) { $has_ordered = false; $statuses = array('wc-failed', 'wc-cancelled', 'wc-refunded'); // loop thru orders, if the order is not falled into failed, cancelled or refund then it consider valid foreach($customer_orders as $tmp_order) { $order = wc_get_order($tmp_order->ID); if(!in_array($order->get_status(), $statuses)) { $has_ordered = true; } } // if this customer already ordered, we remove the coupon if($has_ordered == true) { WC()->cart->remove_coupon( $new_cust_coupon_code ); wc_add_notice( sprintf( "Coupon code: %s is only applicable for new customer." , $new_cust_coupon_code), 'error' ); return false; } } else { // customer has no order, so valid to use this coupon return true; } } else { // new user is valid return true; } } }


Maybe you can do a workaround by putting something like this on the site :

With the coupon XXXXXX get 20 % off for your first commande

<a href="https://docs.woothemes.com/document/coupon-management/" rel="nofollow">https://docs.woothemes.com/document/coupon-management/</a>

and in coupon management you put a limitation to 1 per user ?


  • WooCommerce Avoid add to cart for non logged user
  • Admin tool bar in wordpress
  • Show different home page based on user role
  • How can I include Jquery in my Wordpress footer?
  • Additional paypal fee on Woocommerce checkout page
  • Save new value to Laravel session array
  • How to show “regular price” instead of a “discounted price” on mini-cart (Woo)
  • Organizing large javascript files [closed]
  • List images(01.png) and descriptions(01.txt) from directory
  • D3 nodes and links from JSON with nested arrays of children
  • Release, debug version and Authorization Google?
  • jquery mobile loadPage not working
  • Apache 2.4 - remove | delete | uninstall
  • How to delete a row from a dynamic generate table using jquery?
  • Python: how to group similar lists together in a list of lists?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Acquiring multiple attributes from .xml file in c#
  • Free memory of cv::Mat loaded using FileStorage API
  • Trying to get generic when generic is not available
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • How to stop GridView from loading again when I press back button?
  • How to get Windows thread pool to call class member function?
  • How can I remove ASP.NET Designer.cs files?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal