mikenunez
10/3/2018 - 2:54 AM

Woocommerce deny checkout on multiple failed orders

Woocommerce deny checkout on multiple failed orders

<?php
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-failled' // 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');
        }
    }
}
?>