Php 显示/隐藏WooCommerce中各种产品类别的付款方式
因此,我已经看到了如何根据产品类别取消设置支付网关的示例,例如这一个非常有效的示例:Php 显示/隐藏WooCommerce中各种产品类别的付款方式,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,因此,我已经看到了如何根据产品类别取消设置支付网关的示例,例如这一个非常有效的示例: add_filter( 'woocommerce_available_payment_gateways', 'unset_gateway_by_category' ); function unset_gateway_by_category( $available_gateways ) { if ( is_admin() ) return $available_gateways; if ( !
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忍者,所以希望能得到一些指导
提前谢谢
更新:
下面的版本是迄今为止最接近的版本
当购物车中没有受限制的产品时,它会隐藏专家网关。
当购物车中只有受限制的产品时,它会隐藏常规网关
然而。。。如果有常规产品和限制性产品的mic。。。它隐藏了所有的网关
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;
}
我假设它需要某种规则,比如说,如果购物车混合了常规产品和限制性产品,那么限制性产品规则将应用并覆盖其他规则
谢谢是的,您的代码中有一些错误的逻辑重新设置来取消支付网关。我已经修改了代码,并提供了对修改代码的注释 请应用代码,并让我知道它是否有效。 希望你的问题一定会得到解决
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;
}
这已经在正确的路径上了,但是如果你检查你的代码,最后你只是返回$available\u gateways,没有任何更改…@Reigel实际上我的代码中有一个输入错误,我已经更新了,谢谢你指出。谢谢你的回复@MominIqbal有点困惑在你的例子中11900是从哪里来的?可能是我的错,在我对用例的解释中。试图澄清。这个商店的使用案例,他们有一些CBD产品,他们必须通过专业的支付网关,所以。。。Just regular products=常规支付网关unset specialist gateway Just CBD产品,或常规和CBD产品的组合=specialist gateway unset常规网关。这说明了我想要达到的目标吗?@MominIqbal哦,等等。。。我想我明白了$category_payment_map=数组“11810”=>“worldpay”,“11900”=>“ppec_paypal”;所以,我会把所有产品类别ID都放在这里,每组后面都是我不想为该类别组显示的付款方式?对于特定类别,如果要隐藏多个付款网关,则可以对同一类别使用数组格式。请参阅我的更新答案,以了解由$payment\u gateway\u idFixed生成的相同键入错误$$payment\u gateway\u id。谢谢然而,所有的工作都很好,直到你有一个混合了常规和限制产品的购物车。在这种情况下,它会关闭所有支付网关。很高兴听到它工作正常。Thanks@MominIqbal不,你误解了,它不好用。如果您的购物车中既有常规产品,也有受限制的产品,则它会隐藏所有网关。