【问题标题】:Disable WooCommerce Payment methods if cart item quantity limit is reached如果达到购物车商品数量限制,则禁用 WooCommerce 付款方式
【发布时间】:2017-07-11 08:59:43
【问题描述】:

如果购物车数量增加超过“X 件商品”(例如“15”),是否有办法或过滤器禁用选择性付款方式?

我知道我们可以在添加到购物车之前限制最大数量,但我只想禁用某些付款方式。

谢谢

【问题讨论】:

    标签: php wordpress woocommerce cart product-quantity


    【解决方案1】:

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

    代码如下:

    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+ 版本上运行。

    【讨论】:

    • 是否可以在 $qty_limit 中传递“自定义字段?以便我可以更改每个产品的限制?我在上面创建了这个函数,但我认为我的逻辑不好。函数 woocommerce_product_expire( ) { // 在循环中获取当前帖子 global $post; //调用帖子 max purchase limit acf 字段 $qty_limit = the_field('max_purchase_limit', $post->ID); }
    • 或者如果类别 6 然后限制“10”,则可以将其定义为类别,然后限制“5”。这段代码可以吗?
    【解决方案2】:

    您可以在付款条件中指定如果篮子数量超过您选择的金额(例如,15),则该付款方式将不会显示在拍卖中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-10
      • 2021-12-21
      • 1970-01-01
      • 2018-01-11
      • 2020-08-09
      • 2022-10-04
      • 1970-01-01
      相关资源
      最近更新 更多