Created
January 23, 2014 13:51
-
-
Save stevegrunwell/8578709 to your computer and use it in GitHub Desktop.
(Quick and dirty) modification of http://stevegrunwell.com/blog/woocommerce-restrict-shipping/ to help Katie from http://noizepro.com/ with limiting WooCommerce shipping for two shipping methods.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Return an array of restricted shipping locations for WooCommerce | |
* | |
* Restricted locations include Alaska, American Samoa, Guam, Hawaii, North Mariana Islands, Puerto Rico, | |
* US Minor Outlying Islands, and the US Virgin Islands | |
* | |
* @return array | |
*/ | |
function grunwell_get_restricted_shipping_locations() { | |
return array( 'AK', 'AS', 'GU', 'HI', 'MP', 'PR', 'UM', 'VI' ); | |
} | |
class Grunwell_Shipping_Free_Shipping extends WC_Shipping_Free_Shipping { | |
/** | |
* Limit the availability of this shipping method based on the destination state | |
* | |
* @param array $package | |
* @return bool | |
*/ | |
public function is_available( $package ) { | |
$restricted = grunwell_get_restricted_shipping_locations(); | |
if ( in_array( $package['destination']['state'], $restricted ) ) { | |
return false; | |
} | |
return parent::is_available( $package ); | |
} | |
} | |
class Grunwell_Shipping_Flat_Rate extends WC_Shipping_Flat_Rate { | |
/** | |
* Limit the availability of this shipping method based on the destination state | |
* | |
* @param array $package | |
* @return bool | |
*/ | |
public function is_available( $package ) { | |
$restricted = grunwell_get_restricted_shipping_locations(); | |
if ( in_array( $package['destination']['state'], $restricted ) ) { | |
return false; | |
} | |
return parent::is_available( $package ); | |
} | |
} | |
/** | |
* Make WooCommerce load Grunwell_Shipping_Free_Shipping instead of WC_Shipping_Free_Shipping | |
* @param array $classes An array of classes assembled by Woocommerce::core_shipping | |
* @return array Our filtered $classes | |
*/ | |
function grunwell_override_free_shipping_class( $classes ) { | |
if ( $key = array_search( 'WC_Shipping_Free_Shipping', $classes ) ) { | |
$classes[ $key ] = 'Grunwell_Shipping_Free_Shipping'; | |
} | |
if ( $key = array_search( 'WC_Shipping_Flat_Rate', $classes ) ) { | |
$classes[ $key ] = 'Grunwell_Shipping_Flat_Rate'; | |
} | |
return $classes; | |
} | |
add_filter( 'woocommerce_shipping_methods', 'grunwell_override_free_shipping_class', 11 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment