Disable WooCommerce Payment methods if cart item quantity limit is reached

天大地大妈咪最大 提交于 2019-12-08 05:21:07

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/45029911/disable-woocommerce-payment-methods-if-cart-item-quantity-limit-is-reached

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