Last active
August 29, 2015 14:23
-
-
Save Yame-/4616b3bc75813ab3325b to your computer and use it in GitHub Desktop.
Automatically add a coupon / global discount to a WooCommerce cart.
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
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