Created
August 30, 2021 19:00
-
-
Save divienginesupport/ff3c1a80c231a2b95ba4234a6d88fe1f to your computer and use it in GitHub Desktop.
Automatically add a coupon code on the checkout page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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