Skip to content

Instantly share code, notes, and snippets.

@xadapter
Forked from Nishadup/function.php
Last active March 12, 2020 10:21
Show Gist options
  • Save xadapter/d037fba28087ed80299a53913ba50d38 to your computer and use it in GitHub Desktop.
Save xadapter/d037fba28087ed80299a53913ba50d38 to your computer and use it in GitHub Desktop.
Snippet to add extra weight with every package of the UPS rate request. WooCommerce UPS Shipping Plugin: https://www.pluginhive.com/product/woocommerce-ups-shipping-plugin-with-print-label/
add_filter( 'wf_ups_rate_request', 'adjust_package_weight', 20, 2);
function adjust_package_weight( $rate_request_data, $package ){
$package_extra_weight = 5;
$req_arr = explode('<?xml version="1.0" ?>', $rate_request_data);
$xml_obj = new SimpleXMLElement( $req_arr[2] );
foreach ($xml_obj->Shipment->Package as $key => $package) {
$package->PackageWeight->Weight += $package_extra_weight;
}
$doc = new DOMDocument();
$doc->formatOutput = TRUE;
$doc->loadXML($xml_obj->asXML());
$req_arr[2] = str_replace( '<?xml version="1.0"?>', '', $doc->saveXML() ) ;
return implode('<?xml version="1.0" ?>', $req_arr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment