Created
September 14, 2016 09:36
-
-
Save WooForce/16c62b800476a9c603d74ddb04ff36b0 to your computer and use it in GitHub Desktop.
FedEx - Show only flat rate for specified class ids except specified states
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
add_filter('woocommerce_package_rates', 'show_flat_rate_for_specified_class_id', 10, 2); | |
function show_flat_rate_for_specified_class_id($available_shipping_methods, $package) | |
{ | |
$exceptional_states = array( | |
'CA', | |
'AZ', | |
'NV', | |
'NM' | |
); | |
//Method ids that need to display for specified class ids | |
$shipping_flat_rate_services = array( | |
'flat_rate' | |
); | |
$shipping_fedex_services = array( | |
'wf_fedex_woocommerce_shipping' | |
); | |
// Shipping class IDs that need the method removed | |
$shipping_class_ids = array( | |
6, | |
7, | |
8 | |
); | |
if(!in_array(WC()->customer->shipping_state,$exceptional_states)){ | |
$shipping_class_exists = false; | |
foreach(WC()->cart->cart_contents as $key => $values) { | |
if (in_array($values['data']->get_shipping_class_id() , $shipping_class_ids)) { | |
$shipping_class_exists = true; | |
break; | |
} | |
} | |
if ($shipping_class_exists) { | |
foreach($shipping_fedex_services as & $value) { | |
foreach($available_shipping_methods as $shipping_method=>$method ){ | |
if($method->method_id == $value){ | |
unset($available_shipping_methods[$shipping_method]); | |
} | |
} | |
} | |
}else{ | |
foreach($shipping_flat_rate_services as & $value){ | |
foreach($available_shipping_methods as $shipping_method=>$method ){ | |
if($method->method_id == $value){ | |
unset($available_shipping_methods[$shipping_method]); | |
} | |
} | |
} | |
} | |
}else{ | |
foreach($shipping_flat_rate_services as & $value){ | |
foreach($available_shipping_methods as $shipping_method=>$method ){ | |
if($method->method_id == $value){ | |
unset($available_shipping_methods[$shipping_method]); | |
} | |
} | |
} | |
} | |
return $available_shipping_methods; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment