Skip to content

Instantly share code, notes, and snippets.

@xadapter
Last active September 19, 2019 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xadapter/ba600aa9fbf72b5409cf64493070c45b to your computer and use it in GitHub Desktop.
Save xadapter/ba600aa9fbf72b5409cf64493070c45b to your computer and use it in GitHub Desktop.
Snippet to hide Multi-Carrier Shipping fallback rates if any other carrier rates are being shown. This will work when fallback rate is based on per unit quantity. Multi-Carrier Shipping Plugin for WooCommerce by PluginHive - https://www.pluginhive.com/product/multiple-carrier-shipping-plugin-woocommerce/
/**
* Snippet to hide multicarrier shipping fallback rates if any other multicarrier rates are being shown. This will work when fallback rate is based on per unit quantity.
* Created at : 03 Aug 2018
* Updated at : 03 Aug 2018
* PluginHive Plugins : https://www.pluginhive.com/plugins/
* Gist Link : https://gist.github.com/xadapter/ba600aa9fbf72b5409cf64493070c45b
*/
add_filter( 'woocommerce_package_rates', 'ph_hide_mc_fallback_rates', 10, 2 );
if( ! function_exists('ph_hide_mc_fallback_rates') ) {
function ph_hide_mc_fallback_rates( $shipping_costs, $package ) {
$fallback_rate = 10; // Provide the fallback rate
$quantity = 0;
foreach( $package['contents'] as $line_item ) {
$quantity += $line_item['quantity'];
}
$fallback_cost = $fallback_rate * $quantity;
$total_no_of_mc_shipping_rates = 0;
$total_fallback_rates = 0;
foreach( $shipping_costs as $key => $shipping_cost ) {
$shipping_method_id = $shipping_cost->get_method_id();
if( $shipping_method_id == 'wf_multi_carrier_shipping') {
$total_no_of_mc_shipping_rates++;
if( (float)$fallback_cost == (float)$shipping_cost->get_cost() ) {
$total_fallback_rates++;
$mc_fallback_keys[] = $key;
}
}
}
if( $total_fallback_rates != $total_no_of_mc_shipping_rates && ! empty($total_fallback_rates) ) {
foreach( $mc_fallback_keys as $key ) {
unset($shipping_costs[$key]);
}
}
return $shipping_costs;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment