WooCommerce: Deny Checkout if User Has Pending Orders
As WooCommerce snippet requests by #BloomerArmada fans keep coming to my inbox, it’s time to publish a very useful functionality.
Today, we’ll see how to deny purchasing to a given billing email address, if this is a user and happens to have a pending order already!
Deny checkout if user has a pending order in WooCommerce
PHP Snippet: Deny Checkout if User Has Pending Orders | WooCommerce
/**
* @snippet Deny Checkout to User With Pending Orders | WooCommerce
* @how-to Get CustomizeWoo.com FREE
* @sourcecode https://businessbloomer.com/?p=55387
* @author Rodolfo Melogli
* @testedwith WooCommerce 3.0.5
*/
add_action('woocommerce_after_checkout_validation', 'bbloomer_deny_checkout_user_pending_orders');
function bbloomer_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');
}
}
}
Related posts:
WooCommerce: Limit Shipping to One State Only WooCommerce allows you to limit shipping by countries (or “allowed” countries). However, say your business is based in Pennsylvania, USA...
Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.
If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!
Need Help with WooCommerce?
Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!
Rodolfo Melogli
Business Bloomer Founder
Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.
my question is if a user has a pending order show a message in a popup or otherplace and tell them to go to myorder and complete the order. How can i deliver this message.
Hi Yakub, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi Rodolfo and thank you for all your useful snippets.
Something I don’t get or have missed, I understood how to display such or such notice to the customer but how to disable the payment stuff.
With all the tests I have done, I still can proceed to the payment. Where is the flag to forbide to proceed the payment?
I don’t see any “false” in the various snippets you shared.
Once again, I missed maybe something.
Thank you
Fred
Hey Sandro, thanks so much for your comment! It definitely should work, I don’t see why not. Users must be registered (no guest users) and their order status must be “pending” (not “on-hold” etc.). Let me know
You can actually just grab the current WP user instead of get a billing email. You can also just do a direct PHP count() on the orders to get the number of pending orders. Cleaned it up a little with these two items.
add_action('woocommerce_before_checkout_form', 'bbloomer_deny_checkout_user_pending_orders');
function bbloomer_deny_checkout_user_pending_orders( $posted ) {
global $woocommerce;
// Get current customer/user
$customer = wp_get_current_user();
// If user is not empty, get orders
if (!empty($customer)) {
$customer_orders = get_posts(
array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $customer->ID,
'post_type' => 'shop_order', // WC orders post type
'post_status' => 'wc-pending' // Only orders with status "completed"
)
);
// Check number of pending customer orders
if ( count($customer_orders) > 0 ) {
wc_add_notice('Sorry, please pay your pending orders first by logging into your account', 'error');
}
}
}
Can we deny if only customer have 2 pending order (not one in snippet). Sometimes customer made split order in their purchasing, 2 pending order is more convenient treat.
Very useful, thank you!
I’m trying to find a way to do something similar…. I need to deny the checkout (or hide the add to card button in the single products page) if a customer has already bought ( in other orders) a certain product in the past and try to buy it again.
Hi Rodolfo
I’m a bit confused here apologies. If a customer selects items to purchase and then checks out before completion, then transaction stops. So should they return the process just begins again. I can see merit in previous post ‘re b2b where credit is involved.
I might not be getting this.
No worries Denis! So my thinking is that if a user goes to checkout, completes all the info, goes to the payment page and does not complete the order – then I would like to block such user to keep placing orders until they pay the previous. Of course, this will need to be coupled with an email reminder “please pay your pending order” before stopping them from placing another one.
Note that the order will go into “pending” only if they click on “proceed to payment” button e.g. they go to PayPal ๐
Would be cool if this can be extended to accomodate a custom field “max_allowed_credit” which can be set at user profile by merchant.
This way you can use it in a B2B environment where clients often can buy with conditions like invoice 30 days net. But when the total amount is larger than X EUR/US$, then show this notification.
Another cool trick would be to dynamically change the available payment methods.
As long as they have no pending orders/payments, allow payment via COD/check/invoice net x days. If they have, then hide those options and only show prepaid methods like paypal, creditcard, etc…
Questions? Feedback? Support? Leave your Comment Now! _____
If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)
With 100,000 (and growing) monthly organic sessions, Business Bloomer is the most consistent, most active and most complete WooCommerce development/customization blog.
Of course this website itself uses the WooCommerce plugin, the Storefront theme and runs on a WooCommerce-friendly hosting.
Good day.
The Message pops up, but I can stil proceed with checkout. Am I missing sommeting?
Hi Aldo, are you on the default WooCommerce checkout page?
my question is if a user has a pending order show a message in a popup or otherplace and tell them to go to myorder and complete the order. How can i deliver this message.
Hi Yakub, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Why don’t you use:
instead of:
This is a fastest way.
Excellent, thank you!
Hi Rodolfo and thank you for all your useful snippets.
Something I don’t get or have missed, I understood how to display such or such notice to the customer but how to disable the payment stuff.
With all the tests I have done, I still can proceed to the payment. Where is the flag to forbide to proceed the payment?
I don’t see any “false” in the various snippets you shared.
Once again, I missed maybe something.
Thank you
Fred
Thanks for that Fred. For some reason I thought displaying the error was also denying payment, but if you tested and it doesn’t then another piece of code is required. Use this for inspiration: https://businessbloomer.com/woocommerce-deny-checkout-user-pending-orders/
Is it possible to also include guest users in this based on the fact you are checking vs email?
Sorry, kind of a n00b but trying to work this function into exactly what I need ๐
I don’t think so! Sorry ๐
Hi
Can you tell me if the code still works?
I used in wordpress version 4.9.4, woocommerce version 3.3.3 and php 7.1. Hostgator hosting.
The theme, is a free of the wordpress repository, is called Envo Business.
I did some tests, and it did not work around here.
Hey Sandro, thanks so much for your comment! It definitely should work, I don’t see why not. Users must be registered (no guest users) and their order status must be “pending” (not “on-hold” etc.). Let me know
You can actually just grab the current WP user instead of get a billing email. You can also just do a direct PHP count() on the orders to get the number of pending orders. Cleaned it up a little with these two items.
Thanks so much Bobbie, much appreciated!
Cool snippet. Bravo!
Can we deny if only customer have 2 pending order (not one in snippet). Sometimes customer made split order in their purchasing, 2 pending order is more convenient treat.
Thanks rodolfo, youre cool. Kind regards
Thanks Thomas! Yes, of course, that’s why I put a counter in the PHP – just change that to:
Very useful, thank you!
I’m trying to find a way to do something similar…. I need to deny the checkout (or hide the add to card button in the single products page) if a customer has already bought ( in other orders) a certain product in the past and try to buy it again.
Thanks for your comment Anna! Here you go: https://businessbloomer.com/woocommerce-check-current-user-already-purchased-product/
Hi Rodolfo
I’m a bit confused here apologies. If a customer selects items to purchase and then checks out before completion, then transaction stops. So should they return the process just begins again. I can see merit in previous post ‘re b2b where credit is involved.
I might not be getting this.
No worries Denis! So my thinking is that if a user goes to checkout, completes all the info, goes to the payment page and does not complete the order – then I would like to block such user to keep placing orders until they pay the previous. Of course, this will need to be coupled with an email reminder “please pay your pending order” before stopping them from placing another one.
Note that the order will go into “pending” only if they click on “proceed to payment” button e.g. they go to PayPal ๐
Interesting snippet Rodolfo!
Would be cool if this can be extended to accomodate a custom field “max_allowed_credit” which can be set at user profile by merchant.
This way you can use it in a B2B environment where clients often can buy with conditions like invoice 30 days net. But when the total amount is larger than X EUR/US$, then show this notification.
Another cool trick would be to dynamically change the available payment methods.
As long as they have no pending orders/payments, allow payment via COD/check/invoice net x days. If they have, then hide those options and only show prepaid methods like paypal, creditcard, etc…
Cheers Fabio, nice ideas ๐