因此,我看过一个示例,该示例说明了如何根据产品类别(如效果很好)取消设置付款网关:
add_filter( 'woocommerce_available_payment_gateways',
'unset_gateway_by_category' );
function unset_gateway_by_category( $available_gateways ) {
if ( is_admin() ) return $available_gateways;
if ( ! is_checkout() ) return $available_gateways;
$unset = false;
$category_ids = array( 11810 );
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ( $terms as $term ) {
if ( in_array( $term->term_id, $category_ids ) ) {
$unset = true;
break;
}
}
}
if ( $unset == true ) unset( $available_gateways['worldpay'] );
if ( $unset == true ) unset( $available_gateways['ppec_paypal'] );
return $available_gateways;
}
在这种情况下,如果购物车中有任何产品类别ID为11810的产品,请删除付款方式Worldpay和PayPal。
但是,它并不能完全满足我的需求。
因此,如果您具有类别1、2、3和4 ...以及付款方式a,b和c。
产品类别4只能使用支付网关c,产品类别1,2和3只能使用支付网关a和b。
如果购物车中有所有类别的产品(包括类别4),则上面的代码将隐藏付款方式a和b,仅保留网关c(运行正常)。但是...如果购物车 包含类别4中的任何商品,如何取消设置网关c?
我想我在那儿需要一个elsif,但是...我不是php ninja,所以请多多指教。
谢谢。
更新:
下面的版本是迄今为止最接近的版本。
当购物车中没有任何受限产品时,它会隐藏专家网关。 当购物车中只有受限制的产品时,它将隐藏常规网关。
但是...如果有常规产品和受限产品的麦克风...它会隐藏所有网关。
add_filter( 'woocommerce_available_payment_gateways',
'unset_gateway_by_category' );
function unset_gateway_by_category( $available_gateways ) {
if ( is_admin() ) return $available_gateways;
if ( ! is_checkout() ) return $available_gateways;
$unset = false;
$category_ids = array( 15, 166, 11810 );
/*This will dynamically map the payment gateway that need to be hidden for
the specific category id*/
$category_payment_map = array(
'15' => array('totalprocessing'),
'166' => array('totalprocessing'),
'11810' => array('worldpay', 'ppec_paypal')
);
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ( $terms as $term ) {
if ( in_array( $term->term_id, $category_ids ) ) {
/*Based on Term Id, fetch the payment gateway that need to
be hide from the $category_payment_map array*/
$payment_gateway_ids = $category_payment_map[$term->term_id];
/*you can check here whether the $payment_gateway_id is an array or sin*/
foreach ($payment_gateway_ids as $key => $payment_gateway_id) {
unset( $available_gateways[$payment_gateway_id] );
}
}
}
}
return $available_gateways;
}
我认为它需要某种规则,即如果购物车中包含常规产品和受限制产品的组合,那么受限制产品规则将适用并覆盖其他规则?
谢谢
答案 0 :(得分:1)
是的,您的代码中存在一些错误的逻辑表达,无法设置付款网关。我已经修改了代码,并提供了有关已修改代码的注释。
请应用该代码,并让我知道它是否有效。 希望您的问题一定得到解决
add_filter( 'woocommerce_available_payment_gateways', 'unset_gateway_by_category' );
function unset_gateway_by_category( $available_gateways ) {
if ( is_admin() ) return $available_gateways;
if ( ! is_checkout() ) return $available_gateways;
$unset = false;
$category_ids = array( 11810, 11900 );
/*This will dynamically map the payment gateway that need to be hide for the specific category id*/
$category_payment_map = array(
'11810' => array('worldpay', 'ppec_paypal'),
'11900' => array('ppec_paypal')
);
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ( $terms as $term ) {
if ( in_array( $term->term_id, $category_ids ) ) {
/*Based on Term Id, fetch the payment gateway that need to be hide from the $category_payment_map array*/
$payment_gateway_ids = $category_payment_map[$term->term_id];
/*you can check here whether the $payment_gateway_id is an array or sin*/
foreach ($payment_gateway_ids as $key => $payment_gateway_id) {
unset( $available_gateways[$payment_gateway_id] );
}
}
}
}
return $available_gateways;
}