Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatically apply WooCommerce coupons based on cart quantity
<?php
/**
* Automatically apply coupons for bulk discounts
*/
function hildon_bulk_discount_coupons() {
global $woocommerce; // [a]
if (is_cart() || is_checkout()) :
if (hildon_is_individual_coupon_applied() != true) :
$coupon_five_percent = '5% discount'; // [b]
$coupon_ten_percent = '10% discount'; // [b]
$cart_contents_count = $woocommerce->cart->cart_contents_count; // Get cart contents
/**
* Check quantities and add coupons if valid
*
* [1] If cart has 5 or less items
* [a] Remove 5% coupon
* [b] Remove 10% coupon
*
* [2] If cart has between 6 and 11 items
* [a] If 5% coupon is applied, do nothing
* [b] If 10% coupon is applied
* [i] Apply 5% coupon
* [ii] Remove 10% coupon
* [c] If neither coupon is applied
* [i] Add 5% coupon
*
* [3] If cart has 12 or more items
* [a] If 5% coupon is applied
* [i] Remove 5% coupon
* [ii] Apply 10% coupon
* [b] If 10% coupon is applied, do nothing
* [c] If neither coupon is applied
* [i] Add 10% coupon
*/
if ($cart_contents_count <= 5) : // [1]
if ($woocommerce->cart->has_discount($coupon_five_percent)) : $woocommerce->cart->remove_coupon($coupon_five_percent); endif; // [1][a]
if ($woocommerce->cart->has_discount($coupon_ten_percent)) : $woocommerce->cart->remove_coupon($coupon_ten_percent); endif; // [1][b]
elseif ($cart_contents_count >= 6 && $cart_contents_count <= 11) : // [2]
if ($woocommerce->cart->has_discount($coupon_five_percent)) : // [2][a]
// do nothing();
elseif ($woocommerce->cart->has_discount($coupon_ten_percent)) : // [2][b]
$woocommerce->cart->add_discount($coupon_five_percent); // [2][b][i]
$woocommerce->cart->remove_coupon($coupon_ten_percent); // [2][b][ii]
else : // [2][c]
$woocommerce->cart->add_discount($coupon_five_percent); // [2][c][i]
endif;
else : // [3]
if ($woocommerce->cart->has_discount($coupon_five_percent)) : // [3][a]
$woocommerce->cart->remove_coupon($coupon_five_percent); // [3][a][i]
$woocommerce->cart->add_discount($coupon_ten_percent); // [3][a][ii]
elseif ($woocommerce->cart->has_discount($coupon_ten_percent)) : // [3][b]
// do nothing();
else : // [3][c]
$woocommerce->cart->add_discount($coupon_ten_percent); // [3][c][i]
endif;
endif;
else : // If individual coupons are applied, remove default coupons
if ($woocommerce->cart->has_discount($coupon_five_percent)) : $woocommerce->cart->remove_coupon($coupon_five_percent);
elseif ($woocommerce->cart->has_discount($coupon_ten_percent)) : $woocommerce->cart->remove_coupon($coupon_ten_percent);
endif;
endif;
endif;
}
add_action('wp_head', 'hildon_bulk_discount_coupons');
?>
<?php
/**
* Automatically apply coupons for bulk discounts
*
* hildon_is_individual_coupon_applied()
* [a] Get global class for woocommerce
* [b] Get all Woo coupons that are set to individual use
* [c] Loop through the coupons from [b]
* [d] Get the title for each coupon
* [e] Find out whether this coupon is currently applied in cart
* [f] If coupon is applied, return true
* [g] If coupon is not applied, return false
*/
function hildon_is_individual_coupon_applied() {
global $woocommerce; // [a]
$coupon_args = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'asc',
'post_type' => 'shop_coupon',
'post_status' => 'publish',
'meta_key' => 'individual_use',
'meta_value' => 'yes',
);
$coupons = get_posts($coupon_args); // [b]
foreach ($coupons as $coupon) : // [c]
$coupon_title = get_the_title($coupon); // [d]
if ($woocommerce->cart->has_discount($coupon_title)) : return true; // [e][f]
else : return false; // [e][g]
endif;
endforeach;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.