Home / eCommerce / Restrict payment methods based on cart total
Duplicate Snippet

Embed Snippet on Your Site

Restrict payment methods based on cart total

Restrict payment methods based on cart total

Code Preview
php
<?php
add_filter('woocommerce_available_payment_gateways', 'limit_payment_gateway_based_on_cart_total');
function limit_payment_gateway_based_on_cart_total($available_gateways) {
    if (is_admin() || !is_checkout()) {
        return $available_gateways;
    }
    // Get total cart
    $cart_total = WC()->cart->get_total('edit'); // Total cart without formattation
    // Set limit
    $limit = 1000; // Limit in local currency
    // If the total exceeds the limit, remove all gateways except BACS
    if ($cart_total > $limit) {
        foreach ($available_gateways as $gateway_id => $gateway) {
            if ($gateway_id !== 'bacs') { // BACS is the payment id to keep
                unset($available_gateways[$gateway_id]);
            }
        }
    }
    return $available_gateways;
}

Comments

Add a Comment