WordPress.org

Support

Support » How-To and Troubleshooting » Woocommerce check if coupon is applied

Woocommerce check if coupon is applied

  • Anyone know how I can check if a coupon has been applied to the Woocommerce checkout so that I can do something if it is?

    This is what I have but it does not seem to work, however if I remove the === $coupon_id from the if statement it works but does so for ALL coupons and not the specific coupon I am checking for.

    add_action('woocommerce_before_cart_table', 'apply_product_on_coupon');
    function apply_product_on_coupon( ) {
        global $woocommerce;
    	$coupon_id = '12345';
    
    		if( $woocommerce->cart->applied_coupons === $coupon_id ) {
    		echo 'YAY it works';
        }
    }

    Thanks for your help 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • Where does this code go?
    Can I use it in the review-order.php or checkout.php to check if the visitor has apply a coupon or not. If has, then he can proceed to place order. If not, a notice to say he must fill in coupon code.

    Actually I want to make the coupon field mandatory/required, but I don’t know how to do it and don’t know which field to change.

    Thank you

    mrphpguru
    Member

    @mrphpguru

    Step 1: Create a coupon (http://docs.woothemes.com/document/coupon-management/)

    Step 2: Add this code in your theme function.php
    add_action( ‘woocommerce_before_cart’, ‘apply_matched_coupons’ );

    function apply_matched_coupons() {
    global $woocommerce;

    $coupon_code = ’10percent’; // your coupon code here

    if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;

    if ( $woocommerce->cart->cart_contents_total >= 500 ) {
    $woocommerce->cart->add_discount( $coupon_code );
    $woocommerce->show_messages();
    }

    }

    mrphpguru
    Member

    @mrphpguru

    add_action( ‘woocommerce_before_cart’, ‘apply_matched_coupons’ );

    function apply_matched_coupons() {
    global $woocommerce;

    $get1 = ‘getonech’; // your coupon code here
    $get2 = ‘gettwoch’; // your coupon code here
    $get3 = ‘getthreech’; // your coupon code here
    $get4 = ‘getfourch’; // your coupon code here
    $get5 = ‘getfivech’; // your coupon code here

    if ( $woocommerce->cart->cart_contents_count >= 1 && $woocommerce->cart->cart_contents_count < 2 ) {
    $woocommerce->cart->add_discount( $get1 );
    $woocommerce->show_messages();
    } elseif ( $woocommerce->cart->cart_contents_count >= 2 && $woocommerce->cart->cart_contents_count < 3 ) {
    $woocommerce->cart->add_discount( $get2 );
    $woocommerce->show_messages();
    } elseif ( $woocommerce->cart->cart_contents_count >= 3 && $woocommerce->cart->cart_contents_count < 4 ) {
    $woocommerce->cart->add_discount( $get3 );
    $woocommerce->show_messages();
    } elseif ( $woocommerce->cart->cart_contents_count >= 4 && $woocommerce->cart->cart_contents_count < 5 ) {
    $woocommerce->cart->add_discount( $get4 );
    $woocommerce->show_messages();
    } elseif ( $woocommerce->cart->cart_contents_count >= 5 ) {
    $woocommerce->cart->add_discount( $get5 );
    $woocommerce->show_messages();
    }

    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Woocommerce check if coupon is applied’ is closed to new replies.