Last updated on September 25, 2023

WooCommerce coupon code not applying

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Make sure your WooCommerce coupon codes work flawlessly.

If you are experiencing an issue where a WooCommerce coupon code is not being applied correctly, there are a few possible reasons for this. One common reason is that the coupon code is not valid for the items in the cart. Another reason could be that there is a conflict with another plugin or theme that is preventing the coupon code from being applied.

To troubleshoot and fix this issue, you can use the following code snippet to force the application of a coupon code programmatically:

/**
 * Apply a coupon code programmatically in WooCommerce.
 *
 * @param string $coupon_code The coupon code to apply.
 */
function wpsnippets_apply_coupon_code( $coupon_code ) {
    global $woocommerce;

    // Check if the coupon code is valid.
    if ( $woocommerce->cart->has_discount( $coupon_code ) ) {
        return;
    }

    // Apply the coupon code.
    $woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ) );
}

To use this code snippet, you can call the wpsnippets_apply_coupon_code() function and pass the coupon code as a parameter. The function will check if the coupon code is already applied and return if it is. Otherwise, it will apply the coupon code to the cart using the add_discount() method of the WooCommerce cart object.

This code snippet can be useful if you want to programmatically apply a coupon code in WooCommerce, for example, when a certain condition is met or when a specific action is triggered. It allows you to automate the application of coupon codes and provide a seamless shopping experience for your customers.

Examples

Example 1: Troubleshooting WooCommerce Coupon Code Not Applying

This example demonstrates how to troubleshoot and fix the issue when a WooCommerce coupon code is not applying correctly.

function wpsnippets_fix_coupon_code_not_applying( $valid, $coupon, $discount ) {
    if ( ! $valid && is_wc_endpoint_url( 'checkout' ) ) {
        $valid = true;
    }
    return $valid;
}
add_filter( 'woocommerce_coupon_is_valid', 'wpsnippets_fix_coupon_code_not_applying', 10, 3 );

This code example shows how to use the woocommerce_coupon_is_valid filter to fix the issue of a coupon code not applying. It checks if the coupon is not valid and if the current page is the checkout page, then it sets the coupon as valid. This ensures that the coupon code will be applied even if it was initially marked as invalid.

Example 2: Exclude Specific Products from Coupon Code

This example demonstrates how to exclude specific products from being eligible for a coupon code in WooCommerce.

function wpsnippets_exclude_products_from_coupon( $excluded, $product, $coupon ) {
    $excluded_products = array( 10, 20, 30 ); // Array of product IDs to exclude
    if ( in_array( $product->get_id(), $excluded_products ) ) {
        $excluded = true;
    }
    return $excluded;
}
add_filter( 'woocommerce_coupon_is_valid_for_product', 'wpsnippets_exclude_products_from_coupon', 10, 3 );

This code example uses the woocommerce_coupon_is_valid_for_product filter to exclude specific products from being eligible for a coupon code. It checks if the product ID is in the array of excluded products and sets the product as excluded if it matches. This ensures that the coupon code will not be applied to the excluded products.

Example 3: Limit Coupon Code Usage to Specific User Roles

This example demonstrates how to limit the usage of a coupon code to specific user roles in WooCommerce.

function wpsnippets_limit_coupon_usage_to_user_roles( $valid, $coupon ) {
    $allowed_roles = array( 'customer', 'subscriber' ); // Array of allowed user roles
    $user = wp_get_current_user();
    if ( ! array_intersect( $allowed_roles, $user->roles ) ) {
        $valid = false;
    }
    return $valid;
}
add_filter( 'woocommerce_coupon_is_valid', 'wpsnippets_limit_coupon_usage_to_user_roles', 10, 2 );

This code example uses the woocommerce_coupon_is_valid filter to limit the usage of a coupon code to specific user roles. It checks if the current user’s role is not in the array of allowed roles and sets the coupon as invalid if it doesn’t match. This ensures that only users with the specified roles can apply and use the coupon code.

Last updated on September 25, 2023. Originally posted on October 1, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *