I created a script checking if the user has a a completed order. If the user has no completed order it is disabling the payment method "cheque". This works but after adding it my functions.php i get serious performance problems on my page while navigating thorugh it. Do you see some possiblilites to optimize it or where the problem could be?
function has_bought() {
// Get all customer orders
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order', // WC orders post type
'post_status' => 'wc-completed' // Only orders with status "completed"
) );
// return "true" when customer has already one order
return count( $customer_orders ) > 0 ? true : false;
}
add_filter('woocommerce_available_payment_gateways', 'customize_payment_gateways');
function customize_payment_gateways($gateways) {
if (!has_bought()) {
if (isset($gateways['cheque'])) {
// Unset the 'cheque' payment gateway
unset($gateways['cheque']);
}
}
return $gateways;
}