My e-commerce site sells 99% to South Africans, and we use “BACS” “EFT” & Credit Card as payment options.
We do accept international orders, but can not calculate the shipping cost for international orders automatically.
So when in order is international ( not South Africa ) we want users to select the BACS / EFT payment option. We the ask them to hang-ten with payment so we can confirm the shipping costs first.
BUT if the international order contains only downloadable products ( and therefor do not require shipping ), then we want to make the Credit Card payment option available.
I’m trying to unset a payment method called ‘mygate’ on the checkout page if both these are true:
- The cart contains only downloadable products.
- The shipping address is not South Africa ( ZA )
I’m getting there, but need some help, please.
To disable payment gateway for all countries except South Africa:
function payment_gateway_disable_country( $available_gateways ) { global $woocommerce; if ( isset( $available_gateways['mygate'] ) && $woocommerce->customer->get_country() <> 'ZA' ) { unset( $available_gateways['mygate'] ); } return $available_gateways; } add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
And to check whether the product is downloadable; something todo with:
if( ! $product->is_downloadable() )
Advertisement
Answer
This can be done through a foreach loop where we will check if all products are downloadable. Also oudated get_country()
needs to be replaced by get_shipping_country()
method in this case.
Here is your revisited code:
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country', 20, 1 ); function payment_gateway_disable_country( $available_gateways ) { // Not in backend (admin) if( is_admin() ) return $available_gateways; $only_downloadable_products = true; // Loop through cart items looking for non downloadable products foreach( WC()->cart->get_cart() as $cart_item ){ if( ! $cart_item['data']->is_downloadable() ){ $only_downloadable_products = false; // Non downloadable found break; // Stop the loop } } if( isset( $available_gateways['mygate'] ) && WC()->customer->get_shipping_country() != 'ZA' // <= Changed && $only_downloadable_products ) { // <= Added unset( $available_gateways['mygate'] ); } return $available_gateways; }
Code goes in functions.php file of your active child theme (or active theme).
Tested and and should work for you replacing 'mygate'
by the correct payment gateway ID.