Skip to content

Instantly share code, notes, and snippets.

@varun-pluginhive
Created September 17, 2018 13:02
Show Gist options
  • Save varun-pluginhive/9005d87294abf046243b3ef816185948 to your computer and use it in GitHub Desktop.
Save varun-pluginhive/9005d87294abf046243b3ef816185948 to your computer and use it in GitHub Desktop.
Snippet to add WooCommerce shipping Pro rates to UPS and USPS rates. WOOCOMMERCE TABLE RATE SHIPPING PRO PLUGIN - https://www.pluginhive.com/product/woocommerce-table-rate-shipping-pro-plugin/
/**
* Snippet to add WooCommerce shipping Pro rates to UPS and USPS rates.
* Created at : 17 Sep 2018
* Updated at : 17 Sep 2018
* PluginHive Plugins : https://www.pluginhive.com/plugins/
* Gist Link : https://gist.github.com/varun-pluginhive/9005d87294abf046243b3ef816185948
*/
if( ! function_exists('ph_add_shipping_pro_rate_to_up_or_usps_rates') ) {
function ph_add_shipping_pro_rate_to_up_or_usps_rates( $shipping_rates ) {
$add_to_method = array( "wf_shipping_ups", "wf_shipping_usps" ); // Shipping method to which Shipping Pro rates need to be added
$shipping_pro_rates = false;
$ups_or_usps_rates = false;
$cost_to_add = null;
foreach( $shipping_rates as $rate ) {
$method_id = $rate->get_method_id();
if( $method_id == "wf_woocommerce_shipping_pro" ) {
$cost_to_add = (double) $rate->get_cost();
$shipping_pro_rates = true;
}
elseif( in_array($method_id, $add_to_method ) )
$ups_or_usps_rates = true;
}
if( $shipping_pro_rates && $ups_or_usps_rates ) {
foreach( $shipping_rates as $key=>&$rate ) {
$method_id = $rate->get_method_id();
if( $method_id == "wf_woocommerce_shipping_pro" ) {
unset($shipping_rates[$key]);
}
elseif( in_array($method_id, $add_to_method ) )
$rate->set_cost( (double) $rate->get_cost() + $cost_to_add );
}
}
return $shipping_rates;
}
add_filter( 'woocommerce_package_rates', 'ph_add_shipping_pro_rate_to_up_or_usps_rates' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment