Woocommerce Check Coupons

So this is a situation I had in one of my projects that I need to apply some coupons from a service call but prevent them from being manually applied.

This is one way that I have found to do this Woocommerce check coupons easily.

// Hook the action when a user clicks the apply coupon button
add_action( 'woocommerce_applied_coupon', 'check_coupon' );

function check_coupon() {
	global $woocommerce;

        // Loop through your coupon codes. I suppose you know how to make a coupon from the woocommerce admin.
        // If not see here http://docs.woothemes.com/document/coupon-management/
	$coupons = array(

        // Then we need to find if this coupons are applied to our cart
	$cart_coupons = $woocommerce->cart->applied_coupons;

        // We loop through all the applied coupons
	foreach ( $coupons as $key => $value ) {
		if ( $value == $cart_coupons[ 0 ] ) {
                        // We add an error message
			$woocommerce->add_error( 'Invalid Coupon Code' );

                        // An we remove any discount from the cart. That's all...
			$woocommerce->cart->remove_coupon( $value );
			$woocommerce->cart->discount_total = 0;

Post a commnent