Last active
April 25, 2019 17:50
-
-
Save NickGreen/bd2ff282a4e7b3adc5fcf62271d5298b to your computer and use it in GitHub Desktop.
Auto-apply coupon to cart at least $25
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
<?php | |
// apply free shipping coupon automatically when the cart total is >=$25 | |
add_action('woocommerce_before_cart_table', 'freeship_when_total_at_least_25'); | |
function freeship_when_total_at_least_25() { | |
global $woocommerce; | |
$cart_total = WC()->cart->get_cart_contents_total(); | |
if( $cart_total >= 25 ) { | |
$coupon_code = 'freeshipping'; // this requires the coupon of this name to already exist | |
if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) { | |
$woocommerce->show_messages(); | |
} | |
echo '<div class="woocommerce_message"><strong>Discount has been applied: Free shipping on all initial orders over $25!</strong></div>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Context on how to use this: http://nickgreen.info/auto-add-free-shipping-coupon-for-initial-orders-only-for-woocommerce-subscriptions/