Skip to content

Instantly share code, notes, and snippets.

@WooForce
Created June 21, 2016 09:12
Show Gist options
  • Save WooForce/aa68d1e1906d8e6ceb777c74a2f6ce04 to your computer and use it in GitHub Desktop.
Save WooForce/aa68d1e1906d8e6ceb777c74a2f6ce04 to your computer and use it in GitHub Desktop.
To use a particular shipping method based on order total weight
add_filter( 'wf_rate_for_service', 'wf_force_rate_on_weight', 10, 2 );
function wf_force_rate_on_weight($rates, $packages){
$weight_less_than[] = array('weight'=>1, 'service_code'=>'US-EMI', 'package_type'=>'USPS flat rate envelope'); //Enter proper values or remove this line
$weight_greater_than[] = array('weight'=>1, 'service_code'=>'US-EMI', 'package_type'=>'Package'); //Enter proper values or remove this line
$weight_equal_to[] = array('weight'=>1, 'service_code'=>'US-EMI', 'package_type'=>'USPS flat rate envelope'); //Enter proper values or remove this line
$weight_less_than[] = array('weight'=>1, 'service_code'=>'US-XM', 'package_type'=>'USPS Legal Flat Rate Envelope'); //Enter proper values or remove this line
$weight_greater_than[] = array('weight'=>1, 'service_code'=>'US-XM', 'package_type'=>'Package'); //Enter proper values or remove this line
$weight_equal_to[] = array('weight'=>1, 'service_code'=>'US-XM', 'package_type'=>'USPS Legal Flat Rate Envelope'); //Enter proper values or remove this line
//find total cart weight
$total_weight = 0;
foreach ($packages as $key => $package) {
$total_weight += ( $package['request']['Rate']['WeightLb'] + ( $package['request']['Rate']['WeightOz'] ) / 10 ) * $package['quantity'];
}
//process less than
foreach ($weight_less_than as $key => $value) {
if( $total_weight < $value['weight'] ){
//if the given package_type is available for given service.
if( isset( $rates[ $value['service_code'] ] ) && array_key_exists( $value['package_type'], $rates[ $value['service_code'] ] ) ){
foreach ( $rates[ $value['service_code'] ] as $package_type => $package_type_value ) {
if( $package_type != $value['package_type'] ){
unset( $rates[ $value['service_code'] ][$package_type] );
}
}
}
}
}
//process greater than
foreach ($weight_greater_than as $key => $value) {
if( $total_weight > $value['weight'] ){
//if the given package_type is available for given service.
if( isset( $rates[ $value['service_code'] ] ) && array_key_exists( $value['package_type'], $rates[ $value['service_code'] ] ) ){
foreach ( $rates[ $value['service_code'] ] as $package_type => $package_type_value ) {
if( $package_type != $value['package_type'] ){
unset( $rates[ $value['service_code'] ][$package_type] );
}
}
}
}
}
//process eaqual to
foreach ($weight_equal_to as $key => $value) {
if( $total_weight = $value['weight'] ){
//if the given package_type is available for given service.
if( isset( $rates[ $value['service_code'] ] ) && array_key_exists( $value['package_type'], $rates[ $value['service_code'] ] ) ){
foreach ( $rates[ $value['service_code'] ] as $package_type => $package_type_value ) {
if( $package_type != $value['package_type'] ){
unset( $rates[ $value['service_code'] ][$package_type] );
}
}
}
}
}
return $rates;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment