如果达到购物车数量限制,则禁用WooCommerce付款方式 [英] Disable WooCommerce Payment methods if cart item quantity limit is reached

查看:140
本文介绍了如果达到购物车数量限制,则禁用WooCommerce付款方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果购物车数量增加超过 X个项目数(例如 15),是否有一种方法或过滤器来禁用选择性付款方式?

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.

谢谢

推荐答案

您可以使用连接在 woocommerce_available_payment_gateways 过滤器挂钩。您必须在其中设置数量限制和付款方式。

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.

这是代码:

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;
}

代码包含在您活动的子主题的functions.php文件中(

此代码经过测试,可在WooCommerce 2.6和3+版本上使用。

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

这篇关于如果达到购物车数量限制,则禁用WooCommerce付款方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆