One of my customer request to offer a coupon for all new customer / first order only. But i do not manage to find any free plugin that able to achieve this. So i decided to dig into woocommerce checkout process and write my own function to achieve this.
To apply coupon for new customer in Woocommerce, follow the steps below:-
Advertisements
- Before apply the code below, you have to create a new COUPON. I’ve created a new coupon name ‘firstlove’
- Now you can copy and paste the code below into your theme functions.php
/** * Frontend validate new customer only coupon code * hook: woocommerce_after_checkout_validation */ add_action('woocommerce_after_checkout_validation','check_new_customer_coupon', 0); function check_new_customer_coupon(){ global $woocommerce; // you might change the firstlove to your coupon $new_cust_coupon_code = 'firstlove'; $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; } } }
- once done, you can try to apply the coupon code for a new customer. It should work as expected.
If the code above could not satisfied your requirement, you can freely modify.
Happy Coding!
Related posts:
Virtue Theme: How to enable slider in shop page?
Woocommerce show product RSS feed by category and tags
How to register a free @microsoft.com email account?
Bootstrap - The Responsive Framework
Fckeditor - File Manager Session Problem in IE7
Virtue Theme: How to disable broken image if featured image is set
How to hide admin bar from non admin in WordPress
How to publish blogger content to my domain
Share this with your friends:-
hello everyone, I’ve change this code for run in my woocommerce. first of all, I changed the add_action to
add_action(‘woocommerce_applied_coupon’,’check_new_customer_coupon’, 0);
so, this way, every time a new cupon has added to cart, the code will run, not just after finish an order.
and I changed $customer_orders
that’s is the result:
$customer_orders = get_posts( array(
‘numberposts’ => -1,
‘meta_key’ => ‘_customer_user’,
‘meta_value’ => get_current_user_id(),
‘post_type’ => wc_get_order_types(),
‘post_status’ => array_keys( wc_get_order_statuses() ),
) );
I don’t know, but, looks like ‘post_type’ => ‘shop_order’, is deprecated, for me dosen’t work
I hope help someone, and sorry for my english, I’m not fluent.
Hello,
I have tried out your code in WooCommerce version 3.06 and unfortunately it is not working.
I just love the idea of having a new customer-first order coupon and I am wondering if you could make it work in the later WC versions?
I would really appreciate it! Thank you so very much 🙂
Thanks mate, I got this working and going to launch a campaign soon!
This section of the code does not work:
WC()->cart->remove_coupon( $new_cust_coupon_code );
The notification will show but the coupon does not get removed from the cart. Is there an update to the code to make this work?
should be working.
Hey man can you please tell me if this code is still good to use with the current WooCommerce?