Deny Checkout if a User Has Pending Orders
In this post we will show you how to prevent a logged in user from placing an order when he already has an order with the “Pending Payment” status. This means that the user can only place another order once he has paid for his previous orders.
First, you need to set the order status. To do this, go to WooCommerce –> Orders on the left menu and click into the related order. Set the order status like this:
After that you can add the following code into your functions.php file. This code will activate the feature to deny checkout for the users with “Pending payment” orders.
add_action('woocommerce_after_checkout_validation', 'arrow_design_deny_checkout_user_pending_orders'); function arrow_design_deny_checkout_user_pending_orders( $posted ) { global $woocommerce; $checkout_email = $posted['billing_email']; $user = get_user_by( 'email', $checkout_email ); if ( ! empty( $user ) ) { $customer_orders = get_posts( array( 'numberposts' => -1, 'meta_key' => '_customer_user', 'meta_value' => $user->ID, 'post_type' => 'shop_order', // WC orders post type 'post_status' => 'wc-pending' // Only orders with status "completed" ) ); foreach ( $customer_orders as $customer_order ) { $count++; } if ( $count > 0 ) { wc_add_notice( 'Sorry, please pay your pending orders first by logging into your account', 'error'); } } }
Conclusion
We hope this Deny Checkout if a User Has Pending Orders post was helpful and you are now able to integrate this also on your website! You can find more articles here:
Read another Woocommerce post : ‘Activate Woocommerce Holiday/Pause Mode‘
Arrow Design, based in Dublin, Ireland, provides quality website design services in Dublin and beyond at affordable prices. If you would like help with implementing the above code, or any wordpress website development project, contact us. We love website design and it shows! We provide custom wordpress plugin development, website design training and lots more.
We do it all, so you don’t have to!
Related Posts
Woocommerce Account Page Hook Guide
In this article you will find a visual hook guide for the Woocommerce Account Pages, like the Login/Register page, the Downloads page or the Orders page.
Woocommerce Single Product Page Hook Guide
In this article you will find a visual hook guide for the Woocommerce Single Product Page. This should help you to quickly and easily find the hook positions on the page.
Woocommerce PHP – Product Categories by Product ID
In this tutorial you will learn how to check if a product is assigned to a tag, a category or a custom taxonomy. You can check if a product is is on the shop page, in the cart, in an order & more.
…We do more, so you can do less 🙂