Woocommerce-如果购物车中有特定的变体,则隐藏付款方式



在Wooccommerce中,如果购物车中有特定的产品变体,我想隐藏信用卡支付选项。请帮忙。

谢谢。

这就是我现在的工作。我为每个变体分配了一个单独的运输类,我想在结账时禁用特定的支付方式。但如果我可以针对特定的属性值,这会容易得多,所以我不必分配运输类。

<?php

add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
$shipping_class_target = 106; // the shipping class ID assigned to specific variations 
$in_cart = false;
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
if ( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
$in_cart = true;
break;
} 
}
if ( $in_cart ) {
unset($available_gateways['cod']); // unset 'cod' 
}
else {
unset($available_gateways['bacs']); // unset 'bacs' 
}
return $available_gateways;
}

如果您想检查购物车中每个项目的变化,您必须查找属性$product->get_attributes(),然后循环这些属性,并获得每个项目的数组键和值。

在这个例子中,我使用了

大小(pa_Size(和小型

add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
$in_cart = false;
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
// See if there is an attribute called 'pa_size' in the cart
// Replace with whatever attribute you want
if (array_key_exists('pa_size', (array) $values['data']->get_attributes() ) ) {
foreach ($values['data']->get_attributes() as $attribute => $variation);
// Replace 'small' with your value.  
if ($variation == 'small') $in_cart = true; //edited
} 
}
if ( $in_cart ) {
unset($available_gateways['cod']); // unset 'cod' 
}
else {
unset($available_gateways['bacs']); // unset 'bacs' 
}
return $available_gateways;
}

最新更新