Search code examples
phpwordpresswoocommercecheckoutcoupon

Mandatory coupon before checkout in WooCommerce (optional: for specific products)


I want to force the customer to add a coupon code before they can go to checkout. I would like it to work with every coupon code and every product in my WooCommerce store.

I am using this code and it is almost solving the problem, but it only works on a single coupon code (freev1)

How is it possible to make it work on every coupon code generated?

add_action( 'woocommerce_check_cart_items', 'mandatory_coupon_code' );
function mandatory_coupon_code() {
    // HERE set your coupon code
    $mandatory_coupon = 'freev1';

    $applied_coupons = WC()->cart->get_applied_coupons();

    // If coupon is found we exit
    if( in_array( $mandatory_coupon, $applied_coupons ) ) return;

    // Not found: display an error notice
    wc_add_notice( __( 'Add coupon before checkout.', 'woocommerce' ), 'error' );
}

Solution

  • Just check if $applied_coupons is empty, when empty add notice. Remove $mandatory_coupon & if ( in_array...

    So you get:

    function action_woocommerce_check_cart_items() {
        // Isset
        if ( WC()->cart ) {
            // Get applied coupons
            $applied_coupons = WC()->cart->get_applied_coupons();
    
            // When empty
            if ( empty ( $applied_coupons ) ) {
                // Not found: display an error notice
                wc_add_notice( __( 'Add coupon before checkout.', 'woocommerce' ), 'error' );   
            }
        }
    }
    add_action( 'woocommerce_check_cart_items', 'action_woocommerce_check_cart_items', 10 );
    

    Update:

    To apply this for specific products in cart, use:

    function action_woocommerce_check_cart_items() {
        // The targeted product ids
        $targeted_ids = array( 30, 815 );
    
        // Flag
        $found = false;
        
        // Isset
        if ( WC()->cart ) {
            // Get applied coupons
            $applied_coupons = WC()->cart->get_applied_coupons();
    
            // When empty
            if ( empty ( $applied_coupons ) ) {
                // Loop through cart items
                foreach ( WC()->cart->get_cart() as $cart_item ) {
                    if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
                        $found = true;
                        break;
                    }
                }
            }
        }
        
        // True
        if ( $found ) {
            // Not found: display an error notice
            wc_add_notice( __( 'Add coupon before checkout.', 'woocommerce' ), 'error' );
        }
    }
    add_action( 'woocommerce_check_cart_items', 'action_woocommerce_check_cart_items', 10 );
    

    Additional question:

    "Will it be possible to use almost same code, but instead make it work from the checkout page and force the use of a coupon before placing a order?"

    You could replace woocommerce_check_cart_items with the woocommerce_checkout_process hook for the checkout page