Skip to content

Instantly share code, notes, and snippets.

@anunay
Created December 25, 2014 18:27
Show Gist options
  • Save anunay/830085a20ac80bff6485 to your computer and use it in GitHub Desktop.
Save anunay/830085a20ac80bff6485 to your computer and use it in GitHub Desktop.
Hide paypal based on shipping methods - Woocommerce
<?php
function alter_shipping_methods($available_gateways){
global $woocommerce;
$chosen_titles = array();
$available_methods = $woocommerce->shipping->get_packages();
$chosen_rates = ( isset( $woocommerce->session ) ) ? $woocommerce->session->get( 'chosen_shipping_methods' ) : array();
foreach ($available_methods as $method)
foreach ($chosen_rates as $chosen) {
if( isset( $method['rates'][$chosen] ) ) $chosen_titles[] = $method['rates'][ $chosen ]->label;
}
if( in_array( 'First-Class Mail&#0174; (USPS)', $chosen_titles ) ) {
unset($available_gateways['paypal']);
}
return $available_gateways;
}
add_action('woocommerce_available_payment_gateways', 'alter_shipping_methods');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment