本文介绍了显示基于Woocommerce中的缺货商品的隐藏付款网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当购物车上有任何待补商品时,我需要隐藏paypal;如果没有任何待补商品,则需要隐藏cod.我的问题是,如果有一个待补项目与一个没有待补项目,我最终会浪费付款处理器
I'm need to hide paypal when there's any backordered item on cart or hide cod if there's not any item to be backordered. My problem here is if there's a item that's backorder together with one that is not, I end up whitout a payment processor
add_filter( 'woocommerce_available_payment_gateways', 'backordered_items_hide_cod', 90, 1 );
function backordered_items_hide_cod( $available_gateways ) {
// Only on front end
if ( is_admin() )
return;
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ){
if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
// Hide payment gateway
unset($available_gateways['paypal']);
} else {
unset($available_gateways['cod']);
break; // Stop the loop
}
}
return $available_gateways;
}
推荐答案
以下功能将为找到的任何缺货商品隐藏paypal,或者,如果没有缺货商品,它将隐藏COD:
The following function will hide paypal for any backordered item found or if there is no backordered items it will hide COD instead:
add_filter( 'woocommerce_available_payment_gateways', 'backordered_items_hide_cod', 90, 1 );
function backordered_items_hide_cod( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
$has_a_backorder = false;
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ){
if( $cart_item['data']->is_on_backorder( $cart_item['quantity'] ) ) {
$has_a_backorder = true;
break;
}
}
if( $has_a_backorder ) {
unset($available_gateways['paypal']);
} else {
unset($available_gateways['cod']);
}
return $available_gateways;
}
代码进入您的活动子主题(活动主题)的functions.php文件中.经过测试,可以正常工作.
Code goes in functions.php file of your active child theme (active theme). Tested and works.
这篇关于显示基于Woocommerce中的缺货商品的隐藏付款网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!