WordPress.org

Forums

Woocommerce check if coupon is applied (4 posts)

  1. Blindacme
    Member
    Posted 1 year ago #

    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 :)

  2. rahyrs
    Member
    Posted 1 year ago #

    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

  3. mrphpguru
    Member
    Posted 11 months ago #

    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();
    }

    }

  4. mrphpguru
    Member
    Posted 11 months ago #

    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();
    }

    }

Topic Closed

This topic has been closed to new replies.

About this Topic