Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Automatically apply coupon to cart WooCommerce
add_action( 'woocommerce_before_cart', 'apply_matched_coupons' );
function apply_matched_coupons() {
global $woocommerce;
$get1 = 'getonech'; // your coupon code here
$get2 = 'gettwoch'; // your coupon code here
$get3 = 'getthreech'; // your coupon code here
$get4 = 'getfourch'; // your coupon code here
$get5 = 'getfivech'; // your coupon code here
if ( $woocommerce->cart->cart_contents_count >= 1 && $woocommerce->cart->cart_contents_count < 2 ) {
$woocommerce->cart->add_discount( $get1 );
$woocommerce->show_messages();
} elseif ( $woocommerce->cart->cart_contents_count >= 2 && $woocommerce->cart->cart_contents_count < 3 ) {
$woocommerce->cart->add_discount( $get2 );
$woocommerce->show_messages();
} elseif ( $woocommerce->cart->cart_contents_count >= 3 && $woocommerce->cart->cart_contents_count < 4 ) {
$woocommerce->cart->add_discount( $get3 );
$woocommerce->show_messages();
} elseif ( $woocommerce->cart->cart_contents_count >= 4 && $woocommerce->cart->cart_contents_count < 5 ) {
$woocommerce->cart->add_discount( $get4 );
$woocommerce->show_messages();
} elseif ( $woocommerce->cart->cart_contents_count >= 5 ) {
$woocommerce->cart->add_discount( $get5 );
$woocommerce->show_messages();
}
}
@jshuadvd

This comment has been minimized.

Show comment Hide comment
@jshuadvd

jshuadvd Jul 15, 2015

Should I just add this to my functions.php file?

Should I just add this to my functions.php file?

@vietartisans

This comment has been minimized.

Show comment Hide comment
@vietartisans

vietartisans Jul 1, 2016

Yes

Yes

@titleman

This comment has been minimized.

Show comment Hide comment
@titleman

titleman Dec 8, 2016

What if the coupon codes are automatically generated? Will this work for Store Credits as well? Thank you!

titleman commented Dec 8, 2016

What if the coupon codes are automatically generated? Will this work for Store Credits as well? Thank you!

@seoindex

This comment has been minimized.

Show comment Hide comment
@seoindex

seoindex Jan 5, 2017

ando what about if i have to apply coupon, forx example, on a minimum order of a single product?

seoindex commented Jan 5, 2017

ando what about if i have to apply coupon, forx example, on a minimum order of a single product?

@arggasasao

This comment has been minimized.

Show comment Hide comment
@arggasasao

arggasasao Apr 8, 2017

Very nice, thank you so much! it´s just that i'm looking for!
I use to do a bundle product. with coupons categories restriction and adding multiple products to cart at once.
nice work, thanks again

arggasasao commented Apr 8, 2017

Very nice, thank you so much! it´s just that i'm looking for!
I use to do a bundle product. with coupons categories restriction and adding multiple products to cart at once.
nice work, thanks again

@ddhara

This comment has been minimized.

Show comment Hide comment
@ddhara

ddhara May 2, 2017

Can I use this code in REST API? If yes then how? Where should I put this code file in my woocommerce?

ddhara commented May 2, 2017

Can I use this code in REST API? If yes then how? Where should I put this code file in my woocommerce?

@bjf5051

This comment has been minimized.

Show comment Hide comment
@bjf5051

bjf5051 Aug 8, 2017

I used code similar to this but it looks like customers can get around this by getting to the highest coupon code then just removing items from their cart since all the rules are ">=". Is there code that can be entered which would follow "<=" to automatically remove coupon codes if the rules are no longer met. i.e. items are removed from the cart? Cheers!

bjf5051 commented Aug 8, 2017

I used code similar to this but it looks like customers can get around this by getting to the highest coupon code then just removing items from their cart since all the rules are ">=". Is there code that can be entered which would follow "<=" to automatically remove coupon codes if the rules are no longer met. i.e. items are removed from the cart? Cheers!

@pmaojo

This comment has been minimized.

Show comment Hide comment
@pmaojo

pmaojo Nov 16, 2017

Can this be done not with quantity but with the total (Before taxes, and transport)?

pmaojo commented Nov 16, 2017

Can this be done not with quantity but with the total (Before taxes, and transport)?

@naliseth

This comment has been minimized.

Show comment Hide comment
@naliseth

naliseth Jan 8, 2018

Hello, i have a question. i configured a rule on my woocomerce->coupon (for example apply a coupon discount when min spend is 27$). Now when I want to pay the bill, always appear: do you have any coupon? please enter the code....... Then i only want that this message appear only when the bill is >27$.
Thanks

naliseth commented Jan 8, 2018

Hello, i have a question. i configured a rule on my woocomerce->coupon (for example apply a coupon discount when min spend is 27$). Now when I want to pay the bill, always appear: do you have any coupon? please enter the code....... Then i only want that this message appear only when the bill is >27$.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment