Tag Archives: WooCommerce

Change the default state and country on the checkout in WooCommerce

/** 
 * Manipulate default state and countries
 * 
 * As always, code goes in your theme functions.php file
 */
add_filter( 'default_checkout_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_state', 'change_default_checkout_state' );
 
function change_default_checkout_country() {
  return 'XX'; // country code
}
 
function change_default_checkout_state() {
  return 'XX'; // state code
}

Hide all other shipping methods when free shipping is available in WooCommerce

add_filter( 'woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2 );
function hide_shipping_when_free_is_available( $rates, $package ) { 	
 	// Only modify rates if free_shipping is present
  	if ( isset( $rates['free_shipping'] ) ) {  	
  		// To unset a single rate/method, do the following. This example unsets flat_rate shipping
  		unset( $rates['flat_rate'] );  		
  		// To unset all methods except for free_shipping, do the following
  		$free_shipping          = $rates['free_shipping'];
  		$rates                  = array();
  		$rates['free_shipping'] = $free_shipping;
	}	
	return $rates;
}