简体   繁体   English

如果购物车中有特定产品,请删除付款网关(Woocommerce)

[英]Remove payment gateway if specific product is in the cart (Woocommerce)

In my Woocommerce setup, I have two payment gateways. 在我的Woocommerce设置中,我有两个付款网关。 If a specific product (id = 1187) is in the cart, I want to show gateway_2 and hide gateway_1 . 如果购物车中有特定产品(id = 1187),我想显示gateway_2并隐藏gateway_1 If that product is NOT in the cart, then show " gateway_1 " and hide gateway_2 . 如果该产品gateway_1购物车中,请显示“ gateway_1 ”并隐藏gateway_2

The code below works if I add product 1187 first. 如果我先添加产品1187,则下面的代码有效。 But, if I first add a product that is NOT "1187", then it shows gateway_1 regardless. 但是,如果我首先添加的产品不是“ 1187”,则无论如何它都显示gateway_1 How can I modify this code so that, no matter what, if ID 1187 is in the cart, then ONLY show gateway_2 ? 如何修改此代码,以便无论如何如果购物车中有ID 1187,则仅显示gateway_2

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 id's in array
$specialItem = array(1187);         

if(in_array($values['product_id'],$specialItem)){   
      unset($gateways['gateway_1']);
      break;
}
else {
    unset($gateways['gateway_2']);
    break;
}

}
return $gateways;
}

The problem with your code is that you break the loop, regardless of the condition. 代码的问题是,无论情况如何,都将break循环。

Possible fix: 可能的解决方法:

$inarray = false;
$specialItem = array(1187);
foreach ($woocommerce->cart->cart_contents as $key => $values ) {//enumerate over all cart contents
    if(in_array($values['product_id'],$specialItem)){//if special item is in it
        $inarray = true;//set inarray to true
        break;//optional, but will improve speed.
    }
}

if($inarray) {//product is in the cart
      unset($gateways['gateway_1']);
} else {//otherwise
    unset($gateways['gateway_2']);
}
return $gateways;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 WooCommerce 中的购物车中删除特定的产品变体 - Remove specific product variation from cart in WooCommerce 在购物车中隐藏特定产品 ID 的“确认”支付网关 - hide "Affirm" payment gateway in cart for a specific product id 仅在特定页面上从 WooCommerce 购物车中删除特定产品 - Remove specific product from WooCommerce cart only on a specific page 在 WooCommerce 中为特定产品隐藏购物车中的“删除项目” - Hide “remove item” from cart for a specific product in WooCommerce 根据特定产品有条件地删除Woocommerce购物车项目 - Remove conditionally Woocommerce cart items based on a specific product 删除 Woocommerce 中特定产品类别的添加购物车按钮 - Remove add cart button in Woocommerce for a specific product category 如果购物车/结帐中有特定变化,则隐藏付款方式 | Woocommerce + 产品附加组件插件 - Hide payment method if specific variation in Cart/Checkout | Woocommerce + Product Add-ons Plugin Woocommerce 如果购物车中有特定产品,则将特定产品添加到购物车 - Woocommerce add to cart a specific product if a specific product is in the cart Woocommerce 中特定支付网关结账时的附加字段 - Additional field on checkout for specific payment gateway in Woocommerce 在Woocommerce中为特定支付网关添加自定义费用 - Add a custom fee for a specific payment gateway in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM