Last active
March 31, 2016 11:59
-
-
Save WooForce/8a6dc127ac22ecfd4d3f to your computer and use it in GitHub Desktop.
Hide shipping rates based on the quantities of items
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', '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