Skip to content

Instantly share code, notes, and snippets.

@WooForce
Last active Mar 31, 2016
Embed
What would you like to do?
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