Programmatic Quantity Based Coupons
-
I’m trying to implement code that will make it so that users can enter a coupon code which will only work once they’ve added a certain quantity or greater to their cart. The code that I have so far is based off of this tutorial:
Here is my code:
<?php function 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; } function bulk_discount_coupons() { global $woocommerce; // [a] if (is_cart() || is_checkout()) { if (is_individual_coupon_applied() != true){ $coupon_five_percent = '5% discount'; // [b] $coupon_ten_percent = 'TOOMANYNUCS'; // [b] $cart_contents_count = $woocommerce->cart->cart_contents_count; // Get cart contents if ($woocommerce->cart->has_discount($coupon_ten_percent)) { $woocommerce->cart->remove_coupon($coupon_ten_percent); } if ($cart_contents_count >= 10) { $woocommerce->cart->add_discount($coupon_ten_percent); } else { echo "less than 10"; } } } } add_action('wp_head', 'bulk_discount_coupons');This code works as far as if the quantity is less than 10, the coupon is no longer applied and the total cart amount reflects. If the quantity is 10 or greater, the coupon is automatically applied and the total cart amount reflects. It works however, I don’t want the coupon to apply automatically. I just want it to be enabled.
With how the code is right now, the user isn’t required to enter the coupon code to receive the discount. Please help me figure out how to enable the coupon but not have it apply automatically. Thanks.
The topic ‘Programmatic Quantity Based Coupons’ is closed to new replies.