Home » WooCommerce: Deny Checkout if User Has Pending Orders

WooCommerce: Deny Checkout if User Has Pending Orders

by Tutor Aspire

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 tutoraspire.com FREE
 * @sourcecode    https://tutoraspire.com/?p=55387
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 3.0.5
 */

add_action('woocommerce_after_checkout_validation', 'tutoraspire_deny_checkout_user_pending_orders');

function tutoraspire_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');
}
}

}

You may also like