Last active
August 23, 2016 11:33
-
-
Save WooForce/e09337bf9178c8b5fabffc634bf0f47a to your computer and use it in GitHub Desktop.
Ignore invalid returned rates for stamps
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('wf_rate_for_package','ignore_invalid_box_rates',10,2); | |
function ignore_invalid_box_rates($response, $package_request){ | |
// stamps package types and sizes | |
$stamps_box_sizes = array( | |
// Domestic | |
'US-FC' => array( | |
// Name of the service shown to the user | |
'name' => 'First-Class Mail®', | |
'package_types' => array( | |
'Package' => array( | |
"length" => 84, | |
"width" => 84, | |
"height" => 84, | |
), | |
'Postcard' => array( | |
'length' => 6, | |
'width' => 4.25, | |
'height' => 0.016, | |
), | |
'Large Envelope or Flat' => array( | |
'length' => 15, | |
'width' => 12, | |
'height' => 0.75, | |
), | |
'Thick Envelope' => array( | |
"length" => 34, | |
"width" => 17, | |
"height" => 17, | |
), | |
'Large Package' => array( | |
"length" => 108, | |
"width" => 108, | |
"height" => 108, | |
), | |
) | |
), | |
'US-XM' => array( | |
// Name of the service shown to the user | |
'name' => 'Priority Mail Express™', | |
'package_types' => array( | |
'Flat Rate Envelope' => array( | |
"length" => 12.5, | |
"width" => 9.5, | |
"height" => 0.25, | |
"weight" => 70, | |
), | |
'Flat Rate Padded Envelope' => array( | |
"length" => 12.5, | |
"width" => 9.5, | |
"height" => 1, | |
"weight" => 70, | |
), | |
'Letter' => array( | |
'length' => 11.5, | |
'width' => 6.125, | |
'height' => 0.25, | |
), | |
'Large Envelope or Flat' => array( | |
'length' => 15, | |
'width' => 12, | |
'height' => 0.75, | |
), | |
'Large Package' => array( | |
"length" => 108, | |
"width" => 108, | |
"height" => 108, | |
), | |
'Legal Flat Rate Envelope' => array( | |
"length" => 9.5, | |
"width" => 15, | |
"height" => 0.25, | |
"weight" => 70, | |
), | |
'Package' => array( | |
"length" => 84, | |
"width" => 84, | |
"height" => 84, | |
), | |
'Thick Envelope' => array( | |
"length" => 34, | |
"width" => 17, | |
"height" => 17, | |
), | |
) | |
), | |
'US-MM' => array( | |
// Name of the service shown to the user | |
'name' => 'Media Mail Parcel', | |
'package_types' => array( | |
'Large Envelope or Flat' => array( | |
'length' => 15, | |
'width' => 12, | |
'height' => 0.75, | |
), | |
'Large Package' => array( | |
"length" => 108, | |
"width" => 108, | |
"height" => 108, | |
), | |
'Package' => array( | |
"length" => 84, | |
"width" => 84, | |
"height" => 84, | |
), | |
'Thick Envelope' => array( | |
"length" => 34, | |
"width" => 17, | |
"height" => 17, | |
), | |
) | |
), | |
'US-LM' => array( | |
// Name of the service shown to the user | |
'name' => "Library Mail", | |
'package_types' => array( | |
'Large Envelope or Flat' => array( | |
'length' => 15, | |
'width' => 12, | |
'height' => 0.75, | |
), | |
'Package' => array( | |
"length" => 84, | |
"width" => 84, | |
"height" => 84, | |
), | |
'Thick Envelope' => array( | |
"length" => 34, | |
"width" => 17, | |
"height" => 17, | |
), | |
) | |
), | |
'US-PP' => array( | |
// Name of the service shown to the user | |
'name' => "USPS Parcel Post", | |
'package_types' => array( | |
'Package' => array( | |
"length" => 84, | |
"width" => 84, | |
"height" => 84, | |
), | |
) | |
), | |
'US-PS' => array( | |
// Name of the service shown to the user | |
'name' => "USPS Parcel Select", | |
'package_types' => array( | |
'Large Package' => array( | |
"length" => 108, | |
"width" => 108, | |
"height" => 108, | |
), | |
'Oversized Package' => array( | |
"length" => 130, | |
"width" => 130, | |
"height" => 130, | |
), | |
'Package' => array( | |
"length" => 84, | |
"width" => 84, | |
"height" => 84, | |
), | |
'Thick Envelope' => array( | |
"length" => 34, | |
"width" => 17, | |
"height" => 17, | |
), | |
) | |
), | |
'US-CM' => array( | |
// Name of the service shown to the user | |
'name' => "USPS Critical Mail", | |
'package_types' => array( | |
'Package' => array( | |
"length" => 84, | |
"width" => 84, | |
"height" => 84, | |
), | |
) | |
), | |
'US-PM' => array( | |
// Name of the service shown to the user | |
'name' => "Priority Mail®", | |
'package_types' =>array( | |
'Flat Rate Box' => array( | |
"length" => 11, | |
"width" => 8.5, | |
"height" => 5.5, | |
"weight" => 70, | |
), | |
'Flat Rate Envelope' => array( | |
"length" => 10, | |
"width" => 5, | |
"height" => 0.25, | |
"weight" => 70, | |
), | |
'Flat Rate Padded Envelope' => array( | |
"length" => 12.5, | |
"width" => 9.5, | |
"height" => 1, | |
"weight" => 70, | |
), | |
'Letter' => array( | |
'length' => 11.5, | |
'width' => 6.125, | |
'height' => 0.25, | |
), | |
'Large Envelope or Flat' => array( | |
"length" => 10, | |
"width" => 6, | |
"height" => 0.25, | |
), | |
'Large Flat Rate Box' => array( | |
"length" => 12, | |
"width" => 12, | |
"height" => 5.5, | |
"weight" => 70, | |
), | |
'Large Package' => array( | |
"length" => 108, | |
"width" => 108, | |
"height" => 108, | |
), | |
'Legal Flat Rate Envelope' => array( | |
"length" => 15, | |
"width" => 9.5, | |
"height" => 0.5, | |
"weight" => 70, | |
), | |
'Oversized Package' => array( | |
"length" => 130, | |
"width" => 130, | |
"height" => 130, | |
), | |
'Package' => array( | |
"length" => 84, | |
"width" => 84, | |
"height" => 84, | |
), | |
'Regional Rate Box A' => array( | |
"length" => 13, | |
"width" => 8.5, | |
"height" => 5.5, | |
), | |
'Regional Rate Box B' => array( | |
"length" => 13.625, | |
"width" => 11.875, | |
"height" => 3.375, | |
), | |
'Small Flat Rate Box' => array( | |
"length" => 8.625, | |
"width" => 5.375, | |
"height" => 1.625, | |
"weight" => 70, | |
), | |
'Thick Envelope' => array( | |
"length" => 34, | |
"width" => 17, | |
"height" => 17, | |
), | |
) | |
), | |
// International | |
'US-EMI' => array( | |
// Name of the service shown to the user | |
'name' => "Priority Mail Express International™", | |
'package_types' => array( | |
'Flat Rate Envelope' => array( | |
"length" => 12.5, | |
"width" => 9.5, | |
"height" => 0.25, | |
"weight" => 4, | |
), | |
'Flat Rate Padded Envelope' => array( | |
"length" => 12.5, | |
"width" => 9.5, | |
"height" => 1, | |
"weight" => 4, | |
), | |
'Large Envelope or Flat' => array( | |
'length' => 15, | |
'width' => 12, | |
'height' => 0.75, | |
), | |
'Large Package' => array( | |
"length" => 108, | |
"width" => 108, | |
"height" => 108, | |
), | |
'Legal Flat Rate Envelope' => array( | |
"length" => 15, | |
"width" => 9.5, | |
"height" => 0.25, | |
"weight" => 4, | |
), | |
'Oversized Package' => array( | |
"length" => 130, | |
"width" => 130, | |
"height" => 130, | |
), | |
'Package' => array( | |
"length" => 84, | |
"width" => 84, | |
"height" => 84, | |
), | |
'Thick Envelope' => array( | |
"length" => 34, | |
"width" => 17, | |
"height" => 17, | |
), | |
) | |
), | |
'US-PMI' => array( | |
// Name of the service shown to the user | |
'name' => "Priority Mail International®", | |
'package_types' => array( | |
'Flat Rate Box' => array( | |
"length" => 14, | |
"width" => 12, | |
"height" => 5.5, | |
"weight" => 70, | |
), | |
'Flat Rate Envelope' => array( | |
"length" => 12.5, | |
"width" => 9.5, | |
"height" => 0.25, | |
"weight" => 4, | |
), | |
'Flat Rate Padded Envelope' => array( | |
"length" => 12.5, | |
"width" => 9.5, | |
"height" => 1, | |
"weight" => 4, | |
), | |
'Large Envelope or Flat' => array( | |
"length" => 15, | |
"width" => 12, | |
"height" => 0.75, | |
), | |
'Large Flat Rate Box' => array( | |
"length" => 12, | |
"width" => 12, | |
"height" => 6, | |
), | |
'Large Package' => array( | |
"length" => 108, | |
"width" => 108, | |
"height" => 108, | |
), | |
'Legal Flat Rate Envelope' => array( | |
"length" => 15, | |
"width" => 9.5, | |
"height" => 0.5, | |
), | |
'Oversized Package' => array( | |
"length" => 130, | |
"width" => 130, | |
"height" => 130, | |
), | |
'Package' => array( | |
"length" => 84, | |
"width" => 84, | |
"height" => 84, | |
), | |
'Small Flat Rate Box' => array( | |
"length" => 5.375, | |
"width" => 8.625, | |
"height" => 1.625, | |
"weight" => 4, | |
), | |
'Thick Envelope' => array( | |
"length" => 34, | |
"width" => 17, | |
"height" => 17, | |
), | |
) | |
), | |
'US-FCI' => array( | |
// Name of the service shown to the user | |
'name' => "First Class Package Service™ International", | |
'package_types' => array( | |
'Letter' => array( | |
'length' => 11.5, | |
'width' => 6.125, | |
'height' => 0.25, | |
), | |
'Large Envelope or Flat' => array( | |
'length' => 15, | |
'width' => 12, | |
'height' => 0.75, | |
), | |
'Large Package' => array( | |
"length" => 108, | |
"width" => 108, | |
"height" => 108, | |
), | |
'Oversized Package' => array( | |
"length" => 130, | |
"width" => 130, | |
"height" => 130, | |
), | |
'Package' => array( | |
"length" => 84, | |
"width" => 84, | |
"height" => 84, | |
), | |
'Thick Envelope' => array( | |
"length" => 34, | |
"width" => 17, | |
"height" => 17, | |
), | |
) | |
) | |
); | |
$pkg = $package_request['Rate']; | |
if(!isset($pkg['Length'])){ // package dimension not found | |
return $response; | |
} | |
if(isset($package_request['Rate'])){ | |
foreach($stamps_box_sizes as $service_key => $stamps_box_size){ | |
foreach($stamps_box_size['package_types'] as $package_type => $package_type_dimension){ | |
//check for fittable box | |
if($pkg['Length']>$package_type_dimension['length'] || $pkg['Width']>$package_type_dimension['width'] || $pkg['Height']>$package_type_dimension['height']){ | |
unset($stamps_box_sizes[$service_key]['package_types'][$package_type]); | |
} | |
} | |
} | |
} | |
if(isset($response->Rates)){ | |
$stamps_rates = $response->Rates->Rate; | |
foreach ( $stamps_rates as $stamps_rate_key => $stamps_rate ) { | |
$service_type = (string) $stamps_rate->ServiceType; | |
$package_type = (string) $stamps_rate->PackageType; | |
if(!isset($stamps_box_sizes[$service_type]['package_types'][$package_type])){ | |
unset($stamps_rates[$stamps_rate_key]); | |
} | |
} | |
} | |
$response->Rates->Rate = $stamps_rates; | |
if(isset($response->Rates)){ | |
$stamps_rates = $response->Rates->Rate; | |
foreach ( $stamps_rates as $stamps_rate ) { | |
$service_type = (string) $stamps_rate->ServiceType; | |
$package_type = (string) $stamps_rate->PackageType; | |
} | |
} | |
return $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment