Skip to content

Instantly share code, notes, and snippets.

@xadapter
Last active March 12, 2020 02:27
Show Gist options
  • Save xadapter/71473722eb88105bf70d6d5d0932aed1 to your computer and use it in GitHub Desktop.
Save xadapter/71473722eb88105bf70d6d5d0932aed1 to your computer and use it in GitHub Desktop.
/**
* Snippet to hide duplicate shipping rates (Name and Cost both should match).
* Created at : 09 Aug 2018
* Updated at : 09 Aug 2018
* PluginHive Plugins : https://www.pluginhive.com/plugins/
* Gist Link : https://gist.github.com/xadapter/71473722eb88105bf70d6d5d0932aed1
*/
add_filter( 'woocommerce_package_rates', function( $shipping_rates ){
$hide_duplicate_rules = array('wf_multi_carrier_shipping'); // Hide Duplicate Rates for these shipping methods
$rates_to_check = array();
foreach( $shipping_rates as $key => $shipping_rate ) {
$method_id = $shipping_rate->get_method_id();
if( in_array( $method_id, $hide_duplicate_rules ) ) {
$rates_to_check[$method_id][$key] = array(
'label' => $shipping_rate->get_label(),
'cost' => $shipping_rate->get_cost(),
);
}
}
if( ! empty($rates_to_check) ) {
foreach( $rates_to_check as $rates_of_particular_service ) {
$array_keys_before_uniqueness = array_keys($rates_of_particular_service);
$unique_array = array_unique( $rates_of_particular_service, SORT_REGULAR );
$unique_array_keys = array_keys($unique_array);
$shipping_ids_to_unset = array_diff( $array_keys_before_uniqueness, $unique_array_keys );
foreach( $shipping_ids_to_unset as $key ) {
unset( $shipping_rates[$key]);
}
}
}
return $shipping_rates;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment