Show hide payment gateways based on backordered items in Woocommerce
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;
}