Skip to content

Instantly share code, notes, and snippets.

@mrgarry043
Created June 5, 2017 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrgarry043/8238fb0f4e3e2ed1828ac68df3fdf1a1 to your computer and use it in GitHub Desktop.
Save mrgarry043/8238fb0f4e3e2ed1828ac68df3fdf1a1 to your computer and use it in GitHub Desktop.
Deny Checkout if User Has Pending Orders in WooCommerce
/**
* @snippet Deny Checkout to User With Pending Orders | WooCommerce
* @sourcecode https://wpglorify.com/?p=1952
* @author Garry Singh
* @testedwith WooCommerce 3.0.7
*/
add_action('woocommerce_after_checkout_validation', 'wpglorify_deny_checkout_user_pending_orders');
function wpglorify_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');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment