如果购物车中有特定产品,请禁用所有付款网关 [英] Disable all payments gateway if there's specifics products in the Cart

查看:68
本文介绍了如果购物车中有特定产品,请禁用所有付款网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在特殊情况下,我想禁用所有付款网关:
我有2种我不想在结帐时与其他任何产品结合使用的特殊产品。

I would like to disable all payments gateways under special situation:
I've 2 special products that I don't want to be combined at checkout with any other product.

让我们说我的特殊 产品ID是 496 484 。所有其他产品都是正常 产品。

Lets say that my "special" products IDs are 496 and 484. All other are "normal" products.


  1. 如果其中的特殊产品之一产品在购物车中,例如,我想禁用 paypal。

  1. if one of these "special" products is in the cart, I want to disable "paypal" for example.

如果客户的购物车中一次有一个特殊 产品和普通 产品,我想禁用所有支付网关,这样他就无法结帐。

if a customer has in his cart, at once, a "special" product and a "normal" product, I want to disable all the payments gateway, so he can't checkout.

这是我的代码:

//disable add to cart if
add_filter( 'woocommerce_available_payment_gateways', 'filter_gateways', 1);

function filter_gateways( $gateways )
{   
    global $woocommerce;

    foreach ($woocommerce->cart->cart_contents as $key => $values ) {   
        // store product IDs in array   
        $nonPPproducts = array(496,484);        

        if (in_array( $values['product_id'], $nonPPproducts ) ) {
            unset($gateways['cod'], $gateways['bacs'], $gateways['cheque'], $gateways['stripe']);
        } elseif ( in_array( $values['product_id'], $nonPPproducts ) && in_array( $values['product_id'] ) ) {           
            unset($gateways['under-review'], $gateways['cod'], $gateways['bacs'], $gateways['cheque'], $gateways['stripe']);
        }
    }

    return $gateways;   
}

但是我不知道为什么只有第一个if语句起作用……换句话说,无论情况如何,除了 正在审核 付款网关之外,所有付款网关都将被禁用。

But I can't figure out why the only first if statement works… In other words whatever the situation, all payment gateways are disabled except under-review payment gateway.

我做错了什么?

如何实现?

What I am doing wrong?
How can I achieve this?

谢谢

推荐答案


已更新为WooCommerce 3 +

Updated for WooCommerce 3+

首先我认为代码中的 in_array($ values ['product_id']) 不能作为正确的条件,因此您的else语句永远不会真正。然后,由于客户可以在购物车中放入许多物品,具体取决于客户的后续选择,因此您的代码将有许多冗余的网关未设置

First I think that in_array( $values['product_id'] ) in your code is not working as a correct condition and so your else statement is never "true". Then as a customer can have many items in his cart, depending on customer successive choices, with your code there will be many redundant gateway unsets

在这里重新访问您的代码(您需要在每个语句中放入未设置的期望网关)

Here it is your code revisited (you will need to put the desire unset gateways in each statement):

add_filter( 'woocommerce_available_payment_gateways', 'filter_gateways', 1);
function filter_gateways( $gateways ){
    // Not in backend (admin)
    if( is_admin() ) 
        return $gateways;

    // Storing special product IDs in an array
    $non_pp_products = array( 496, 484 );

    // Needed variables
    $is_non_prod = false;
    $is_prod = false;
    $count = 0;

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // count number of items if needed (optional) 
        $count++;
        $product = $cart_item['data'];
        if( ! empty($product) ){
            $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
            if ( in_array( $product_id, $non_pp_products ) && ! $is_non_prod ) 
                $is_non_prod = true;

            if ( !in_array( $product_id, $non_pp_products ) && !$is_prod )
                $is_prod = true;

        }
    }
    if ( $is_non_prod && ! $is_prod ) // only special products 
    {
        // unset only paypal;
        unset( $gateways['paypal'] );
    } 
    elseif ( $is_non_prod && $is_prod ) // special and normal products mixed
    {
        // unset ALL GATEWAYS
        unset( $gateways['cod'], 
               $gateways['bacs'], 
               $gateways['cheque'], 
               $gateways['paypal'], 
               $gateways['stripe'], 
               $gateways['under-review'] );
    }
    elseif ( ! $is_non_prod && $is_prod ) // only normal products (optional)
    {
        // (unset something if needed)
    }
    return $gateways; 
}

自然地,此代码在活动孩子的functions.php文件中进行主题或主题。

这篇关于如果购物车中有特定产品,请禁用所有付款网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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