Skip to content

Instantly share code, notes, and snippets.

@divienginesupport
Created August 30, 2021 19:00
Show Gist options
  • Save divienginesupport/ff3c1a80c231a2b95ba4234a6d88fe1f to your computer and use it in GitHub Desktop.
Save divienginesupport/ff3c1a80c231a2b95ba4234a6d88fe1f to your computer and use it in GitHub Desktop.
Automatically add a coupon code on the checkout page
/**
* Apply Coupon Code automatically to the cart
*/
function divi_engine_apply_discount_to_cart() {
$order_total = WC()->cart->get_subtotal();
if( $order_total > 50 ) { //Here you set the condition which in this case is spend over $50
$coupon_code = '20%OFF'; //Here you indicate which coupon code to apply
if ( !WC()->cart->add_discount( sanitize_text_field( $coupon_code ) ) ) {
WC()->show_messages();
}
}
}
add_action( 'woocommerce_before_cart_table', 'divi_engine_apply_discount_to_cart' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment