Stunomatic
Stunomatic

Reputation: 89

Disable WooCommerce Payment methods if cart item quantity limit is reached

Is there a way or filter to disable selective payment methods if cart quantity increase more than "X number of items" example "15"?

I know we can limit max number of quantity before adding to cart but I want to disable some payment methods only.

Thanks

Upvotes: 2

Views: 1953

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 254117

You can use a custom function hooked in woocommerce_available_payment_gateways filter hook. You will have to set inside it your quantity limit and your payment methods slugs.

Here is that code:

add_filter('woocommerce_available_payment_gateways', 'unsetting_payment_gateway', 10, 1);
function unsetting_payment_gateway( $available_gateways ) {
    // Not in backend (admin)
    if( is_admin() ) 
        return $available_gateways;

    // HERE Define the limit of quantity item
    $qty_limit = 15;
    $limit_reached = false;

    // Iterating through each items in cart
    foreach(WC()->cart->get_cart() as $cart_item){
        if($cart_item['quantity'] > $qty_limit ){
            $limit_reached = true;
            break;
        }
    }
    if($limit_reached){
        // HERE set the slug of your payment method
        unset($available_gateways['cod']);
        unset($available_gateways['bacs']);
    }
    return $available_gateways;
}

Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works on WooCommerce version 2.6 and 3+.

Upvotes: 3

Iman Sharifi
Iman Sharifi

Reputation: 69

You can specify in the payment condition that if the basket number exceeds your chosen amount (e.g., 15), then the payment method will not be displayed in the auction.

Upvotes: 0

Related Questions