Skip to content

Instantly share code, notes, and snippets.

@Yame-
Last active August 29, 2015 14:23
Show Gist options
  • Save Yame-/4616b3bc75813ab3325b to your computer and use it in GitHub Desktop.
Save Yame-/4616b3bc75813ab3325b to your computer and use it in GitHub Desktop.
Automatically add a coupon / global discount to a WooCommerce cart.
function global_discount(){
global $woocommerce;
$coupon = 'your-coupon-code-name';
// We'll add the coupon code the first time.
// This way users can remove the coupon code
// when they have a bigger discount coupon.
if( 0 == $woocommerce->cart->cart_contents_count ){
// This coupon can be managed from the coupon dashboard
if( !$woocommerce->cart->has_discount( $coupon ) ) {
$woocommerce->cart->add_discount( $coupon );
}
}
}
// Whenever a users adds a product to the cart.
add_action( 'woocommerce_add_to_cart', 'global_discount' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment