Once again, I’m trying to find WooCommerce anti-spam workarounds to avoid manual admin work like receiving hundreds of emails, deleting hundreds of fake orders and fake WordPress users.
In today’s “episode” I will try to clean the WordPress User database table after a failed order, because I’m really angry when “17PmU3MmKZS9ZAy7 17PmU3MmKZS9ZAy7” manages to register an account on Business Bloomer after a carding attack!
Please test this snippet on a dev/staging environment and not directly on the live site. Deleting a WordPress user is never a good idea, so you need to make sure everything is working as it should. Enjoy!
Ah, carding attacks! Here is a series of fake orders placed by fake users. My goal is to destroy these users and remove them from the WordPress database, so that it remains clean.
PHP Snippet: Delete Customer After a Failed Order @ WooCommerce Checkout
Note 1: this snippet runs on the WooCommerce Thank You page, so make sure your credit card orders are redirected there.
Note 2: this function only works for non-guest orders, because it then uses the logged in user to check if they have previous legit orders.
Note 3: to me, a legit order is when a customer has previous Processing, Completed, Pending or On-hold orders in their name. Feel free to remove or add order statuses to the array.
Note 4: as of now, if this is a first-time customer (so, no previous orders), they will be deleted. It may be a problem for legit customers who end up with a failed order due to a failed payment… Still not sure how to exclude these humans.
Note 5: how can you test this? Because you really don’t want to place a fake order with a real credit card and make it fail… My workaround: from the WP admin I create a new user – then I create a manual WooCommerce order in their name – then set the order status to failed. At this point, I go to the thank you page and the code triggers. User should be deleted, UNLESS the same user has a previous legit order.
/** * @snippet Destroy Failed Order Customer @ WooCommerce Checkout * @how-to Get CustomizeWoo.com FREE * @author Rodolfo Melogli * @compatible WooCommerce 8 * @donate $9 https://businessbloomer.com/bloomer-armada/ */ add_action( ‘woocommerce_thankyou’, ‘bbloomer_destroy_failed_order_customer’ ); function bbloomer_destroy_failed_order_customer( $order_id ) { $order = wc_get_order( $order_id ); if ( $order->has_status( ‘failed’ ) ) { $customer_id = is_callable( array( $order, ‘get_customer_id’ ) ) ? $order->get_customer_id() : 0; if ( $customer_id == 0 ) return; $legit_orders = wc_get_orders( [ ‘customer’ => $customer_id, ‘status’ => array( ‘wc-processing’, ‘wc-completed’, ‘wc-pending’ ), ‘return’ => ‘ids’, ] ); if ( count( $legit_orders ) > 0 ) return; require_once( ABSPATH.’wp-admin/includes/user.php’ ); wp_delete_user( $customer_id ); } }
Where to add custom code?
You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its style.css file. Make sure you know what you are doing when editing such files – if you need more guidance, please take a look at my guide “Should I Add Custom Code Via WP Editor, FTP or Code Snippets?” and my video tutorial “Where to Place WooCommerce Customization?”
Does this snippet (still) work?
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.