Disable WooCommerce Payment methods if cart item quantity limit is reached

百般思念 提交于 2019-12-06 15:20:41

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 ) {

    // 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 function.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+.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!