Disable WooCommerce Payment methods if cart item q

2019-05-31 04:48发布

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

2条回答
\"骚年 ilove
2楼-- · 2019-05-31 05:05

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.

查看更多
冷血范
3楼-- · 2019-05-31 05:15

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

查看更多
登录 后发表回答