1// Show/hide payment gateways
2add_filter( 'woocommerce_available_payment_gateways', 'conditionally_hide_payment_gateways', 100, 1 );
3function conditionally_hide_payment_gateways( $available_gateways ) {
4 // 1. On Order Pay page
5 if( is_wc_endpoint_url( 'order-pay' ) ) {
6 // Get an instance of the WC_Order Object
7 $order = wc_get_order( get_query_var('order-pay') );
8
9 // Loop through payment gateways 'pending', 'on-hold', 'processing'
10 foreach( $available_gateways as $gateways_id => $gateways ){
11 // Keep paypal only for "pending" order status
12 if( $gateways_id !== 'paypal' && $order->has_status('pending') ) {
13 unset($available_gateways[$gateways_id]);
14 }
15 }
16 }
17 // 2. On Checkout page
18 elseif( is_checkout() && ! is_wc_endpoint_url() ) {
19 // Disable paypal
20 if( isset($available_gateways['paypal']) ) {
21 unset($available_gateways['paypal']);
22 }
23 }
24 return $available_gateways;
25}
26