Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Stop automatically applying a coupon after it's been used WordPress

  • SOLVED

Hi!

I have some code in functions.php that applies itself for first time customers:

// apply first-time customer coupon automatically
add_action( 'woocommerce_before_cart', 'apply_matched_coupons' );
add_action( 'woocommerce_before_checkout_form', 'apply_matched_coupons' );

function apply_matched_coupons() {
global $woocommerce;

$coupon_code = 'blahblahnew';

if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;

if ( ($woocommerce->cart->cart_contents_total >= 1) && ( is_user_logged_in() ) ) {
$woocommerce->cart->add_discount( $coupon_code );

}
}


I would like to remove the action of automatically applying the coupon once the customer has made their first purchase and the coupon is no longer valid.

This is what I have so far, but it's not working:

// hide error message after first-time customer has used coupon
add_action( 'woocommerce_before_checkout_form' , 'coupon_remove_error_message' );
add_filter( 'woocommerce_coupon_error', 'coupon_remove_error_message', 10, 2);

function coupon_remove_error_message($err, $err_code) {


global $woocommerce;

$coupon_id = 'blahblahnew';

$applied_coupon = $woocommerce->cart->applied_coupons;

if ($applied_coupon[0] == $coupon_id) && /*coupon has already been used*/ ) {
remove_action( 'woocommerce_before_checkout_form', 'apply_matched_coupons' );
}


}


Thank you!!!

Answers (3)

2016-01-05

Reigel Gallarde answers:

I think you only need to limit the coupon to be used only once per user...

[[LINK href="https://docs.woothemes.com/document/coupon-management/#section-5"]]see this link[[/LINK]]


Melba comments:

Hi,

Yes, I have set the coupon so that it can only be used once per customer. However, because I set up my code so that it applies automatically at checkout, it keeps displaying the error "Usage limit has been reached". I'm trying to remove the action so that once a customer has used the coupon, it no longer applies itself for that customer.

Thanks!


Reigel Gallarde comments:

ok, let's edit your code...

function apply_matched_coupons() {

global $woocommerce;

$coupon_code = 'blahblahnew';

// Get the coupon
$the_coupon = new WC_Coupon( $coupon_code );

// Check it can be used with cart
if ( ! $the_coupon->is_valid() ) return;

if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;

if ( ($woocommerce->cart->cart_contents_total >= 1) && ( is_user_logged_in() ) ) {

$woocommerce->cart->add_discount( $coupon_code );

}

}


I have added two lines... this will check if the coupon is valid for this cart... and will return (do nothing) if coupon is not valid for current cart.


Reigel Gallarde comments:

error "Usage limit has been reached" means, coupon is not valid to be used in the cart.


Melba comments:

It works great! Thank you!!!


Reigel Gallarde comments:

You're welcome! and please don't forget to vote to award the prize for this question ;)

2016-01-05

Romel Apuya answers:

Does removing the coupon apply only for the current user?
Or remove the coupon validity on any user that will be going to use it??


Romel Apuya comments:

Does removing the coupon apply only for the current user?
Or remove the coupon validity on any user that will be going to use it??


Melba comments:

Hi and thank you,

Removing the coupon will be only for the current user. The coupon is set so that it can be used an infinite number of times, but each customer can only use it once. Once a customer has used it (it automatically applies to their first purchase), it's no longer valid for him/her but it's still valid for everyone else that hasn't made any purchases yet.


Melba comments:

Hi and thank you,

Removing the coupon will be only for the current user. The coupon is set so that it can be used an infinite number of times, but each customer can only use it once. Once a customer has used it (it automatically applies to their first purchase), it's no longer valid for him/her but it's still valid for everyone else that hasn't made any purchases yet.

2016-01-05

Shoeb mirza answers:

Go to
Woocommerce > Coupon > Usage Limits > Enter number "1"
Woocommerce > Coupon > Usage limit per user > Enter number "1"


Melba comments:

Hi,

Yes, I have set that up. The coupon can only be used once per customer. But it still continues to apply itself automatically resulting in the error message "Usage Limit has been reached." after a customer has already used it. It still applies fine for other users, which is what I want. I just don't want the coupon to keep applying itself once a specific customer has used it. I hope I'm being clear.