Skip to content

Instantly share code, notes, and snippets.

@WooForce
Last active March 31, 2016 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WooForce/8a6dc127ac22ecfd4d3f to your computer and use it in GitHub Desktop.
Save WooForce/8a6dc127ac22ecfd4d3f to your computer and use it in GitHub Desktop.
Hide shipping rates based on the quantities of items
add_filter( 'woocommerce_package_rates', 'wf_adjust_shipping_rate', 10, 2 );
function wf_adjust_shipping_rate( $available_shipping_methods, $packages ){
// Total quantity is total unit
$unit = 0;
$apply_final_filter = TRUE;
foreach ( $packages['contents'] as $item ) {
$unit+=$item['quantity'];
}
// if unit less than 3 , show D_FIRST_CLASS only
if( $unit < 3 ){
$shipping_methods = array();
if( !empty( $available_shipping_methods['wf_shipping_usps:D_FIRST_CLASS'] ) ){
$apply_final_filter = FALSE;
$shipping_methods['wf_shipping_usps:D_FIRST_CLASS'] = $available_shipping_methods['wf_shipping_usps:D_FIRST_CLASS'];
$available_shipping_methods = $shipping_methods;
}
}// if unit between 3 and 30 , show D_STANDARD_POST only
elseif( $unit > 2 && $unit < 31 ){
$shipping_methods = array();
if(!empty($available_shipping_methods['wf_shipping_usps:D_STANDARD_POST'])){
$apply_final_filter = FALSE;
$shipping_methods['wf_shipping_usps:D_STANDARD_POST'] = $available_shipping_methods['wf_shipping_usps:D_STANDARD_POST'];
$available_shipping_methods = $shipping_methods;
}
}
// if no above conditions met , remove all USPS service
if( $apply_final_filter ){
foreach ($available_shipping_methods as $index => $data) {
if (strpos($index, 'wf_shipping_usps') !== false) {
unset($available_shipping_methods[$index]);
}
}
}
return $available_shipping_methods;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment