Skip to content

Instantly share code, notes, and snippets.

@NickGreen
Last active April 25, 2019 17:50
Show Gist options
  • Save NickGreen/bd2ff282a4e7b3adc5fcf62271d5298b to your computer and use it in GitHub Desktop.
Save NickGreen/bd2ff282a4e7b3adc5fcf62271d5298b to your computer and use it in GitHub Desktop.
Auto-apply coupon to cart at least $25
<?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>';
}
}
@NickGreen
Copy link
Author

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