Created
March 4, 2022 12:54
-
-
Save imanispatel/1eee9304fa5c810276de8328dbc51498 to your computer and use it in GitHub Desktop.
Woocommerce payment gateway: disable payment gateway by shipping method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Woocommerce disable payment gateway by shipping method. | |
*/ | |
add_filter( 'woocommerce_available_payment_gateways', 'venus_payment_method_by_shipping' ); | |
function venus_payment_method_by_shipping( $gateways ) { | |
if( is_admin() ) { // do nothing in /wp-admin | |
return $gateways; | |
} | |
if( is_wc_endpoint_url( 'order-pay' ) ) { // "Pay for order" page | |
$order = wc_get_order( wc_get_order_id_by_order_key( $_GET[ 'key' ] ) ); | |
if( $order->has_shipping_method( 'flat_rate' ) ) { | |
if( isset( $gateways[ 'razorpay' ] ) ) { | |
unset( $gateways[ 'razorpay' ] ); // unset cash on delivery if exists | |
} | |
if( isset( $gateways[ 'simpl-pay-in-3-for-woocommerce' ] ) ) { | |
unset( $gateways[ 'simpl-pay-in-3-for-woocommerce' ] ); // unset cash on delivery if exists | |
} | |
} | |
} else { // Cart/Checkout page | |
// get a shipping method selected by a customer | |
$chosen_shipping_method = WC()->session->get( 'chosen_shipping_methods' )[0]; | |
// check the shipping method | |
if ( 'flat_rate:9' === $chosen_shipping_method ) { | |
if( isset( $gateways[ 'razorpay' ] ) ) { | |
unset( $gateways[ 'razorpay' ] ); | |
} | |
if( isset( $gateways[ 'simpl-pay-in-3-for-woocommerce' ] ) ) { | |
unset( $gateways[ 'simpl-pay-in-3-for-woocommerce' ] ); // unset cash on delivery if exists | |
} | |
} | |
} | |
return $gateways; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment