Skip to content

Instantly share code, notes, and snippets.

@salgua
Created December 5, 2014 11:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save salgua/30ee2bc205d71f758fd9 to your computer and use it in GitHub Desktop.
Save salgua/30ee2bc205d71f758fd9 to your computer and use it in GitHub Desktop.
Woocommerce COD only for specific country with specific shipment rate
//enable COD only for italy
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['cod'] ) && $woocommerce->customer->get_country() <> 'IT' ) {
unset( $available_gateways['cod'] );
}
//make COD the only available gateway for flat_rate shipping method (or other unused method)
//you can use the label cash on delivery for flat_rate and determining the shipping cost for COD
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
if ($chosen_methods[0] == 'flat_rate')
{
//only cod is available
$available_gateways = array('cod' => $available_gateways['cod']);
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
@Im-a-MARVEL
Copy link

Hi, I need to make COD only available for one state in my country along with other gateways. And disable it for other states. What changes do I make here?

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