Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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