Skip to content

Instantly share code, notes, and snippets.

@InpsydeNiklas
Last active June 2, 2023 07:46
Show Gist options
  • Save InpsydeNiklas/163f515cbad8e4668cf4578f319dcd6b to your computer and use it in GitHub Desktop.
Save InpsydeNiklas/163f515cbad8e4668cf4578f319dcd6b to your computer and use it in GitHub Desktop.
Hide the PayPal gateway and smart buttons on Checkout page for countries other than ['US', 'CA', 'PR']
<?php
// PHP function to unset Stripe gateway
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_countries' );
function payment_gateway_disable_countries( $gateways ) {
if ( is_admin() ) {
return $gateways;
}
if ( ! is_object( WC()->customer ) OR ! method_exists( WC()->customer, 'get_billing_country' ) ) {
return $gateways;
}
$paypal_countries = array('US', 'CA', 'PR');
if ( is_wc_endpoint_url( 'order-pay' ) ) { // Pay for order page
$order = wc_get_order( wc_get_order_id_by_order_key( $_GET[ 'key' ] ) );
$country = $order->get_billing_country();
} else { // Cart page
$country = WC()->customer->get_billing_country();
}
if ( in_array( $country, $paypal_countries ) && isset( $gateways['stripe'] ) ) {
unset( $gateways['stripe'] );
}
return $gateways;
}
// JavaScript code to hide PayPal gateway based on country
add_action( 'wp_footer', 'payment_gateway_hide_gateways_based_on_country' );
function payment_gateway_hide_gateways_based_on_country() {
?>
<style>
.hide-gateway {
display: none !important;
}
</style>
<script>
(function($) {
function hidePaymentGateways() {
var paypalCountries = ['US', 'CA', 'PR'];
var country = '';
if (typeof wc_checkout_params !== 'undefined') {
country = $('select#billing_country').val() || wc_checkout_params.default_country;
} else if ($('select#calc_shipping_country').length > 0) {
country = $('select#calc_shipping_country').val();
} else {
return;
}
if (paypalCountries.indexOf(country) === -1) {
$('li.payment_method_ppcp-gateway').addClass('hide-gateway');
$('#ppc-button-ppcp-gateway').addClass('hide-gateway');
} else {
$('li.payment_method_ppcp-gateway').removeClass('hide-gateway');
$('#ppc-button-ppcp-gateway').removeClass('hide-gateway');
}
}
$(document).ready(function() {
hidePaymentGateways();
$('body').on('change', 'select#billing_country, select#calc_shipping_country', hidePaymentGateways);
});
$(document.body).on('updated_checkout', function() {
hidePaymentGateways();
});
})(jQuery);
</script>
<?php
}
@fullaineven
Copy link

THX for your information sir ,sorry because i'm the new user do you know where i can change this code? please help

@InpsydeNiklas
Copy link
Author

Hi @fullaineven you can insert this either into the functions.php file when you are using a child theme or use a plugin like Code Snippets to create a snippet that runs on your site.
But please keep in mind this code example above was written for a quite specific user request. So unless you intend to provide the PayPal Checkout gateway only for US, CA, & PR and Stripe for every other country, this code would need some adjustments.

If you need more assistance with this, I suggest reaching out to the support team directly. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment