In WooCommerce, it can be helpful to prevent users with pending orders from checking out until they have completed or canceled those orders.

This tutorial will guide you through the process of implementing a solution that denies checkout to users with pending orders.

Deny Checkout if User Has Pending Orders in WooCommerce

By following these steps, you can enhance your WooCommerce store’s user experience and streamline the order management process.

Step 1: Access the Theme Functions File or use Code Snippets Plugin

To begin, you’ll need to access your theme’s functions.php file. Alternatively, you can use a Code Snippets plugin specifically for this functionality. Both methods are acceptable, but using a plugin allows for easier management and updates.

Step 2: Add the PHP Snippet

After activating the Code Snippets plugin, you’ll find a new menu item called “Snippets” in your admin dashboard. Go to “Snippets” > “Add New” to create a new snippet.

Then, paste the following code into the code editor:

/**
 * Deny Checkout if User Has Pending Orders in WooCommerce
 * @source https://www.wptechnic.com/?p=11099
 * @compatible WC 7.8.1
 */

add_action('woocommerce_check_cart_items', 'check_pending_orders');

function check_pending_orders() {
    $user_id = get_current_user_id();

    // Get the user's pending orders
    $pending_orders = wc_get_orders(array(
        'status' => 'wc-pending',
        'customer' => $user_id,
    ));

    if (!empty($pending_orders)) {
        wc_add_notice(__('You have pending orders. Please complete or cancel them before checking out.'), 'error');
        return false; // Prevent checkout
    }
}

Once you’ve added the snippet, click the “Save Changes and Activate” button to save and activate the snippet.

and the result is:

How to Deny Checkout if User Has Pending Orders in WooCommerce

Test the Functionality

To test the functionality, add products to the cart and try to proceed to the checkout. If there are pending orders associated with the user, an error notice will be displayed, preventing the checkout process until the pending orders are completed or canceled.


By implementing this solution, you can enhance the user experience of your WooCommerce store by preventing users from checking out while they have pending orders. This approach ensures that customers can properly manage their orders and reduces potential confusion or complications in the ordering process.

That’s it! By following this tutorial, you can successfully deny checkout if a user has pending orders in WooCommerce.

Avatar for Muhammad Tamzid
Author

Muhammad Tamzid, the founder of WPTechnic, is a WordPress Enthusiast with a passion to help beginners learning WordPress. Also managing WPrevival, a 24/7 WordPress Website Development, Maintenance & Security Service company.

Write A Comment