Skip to content

Instantly share code, notes, and snippets.

@xadapter xadapter/functions.php
Last active Feb 28, 2019

Embed
What would you like to do?
/**
* Snippet to hide duplicate shipping rates (Name and Cost both should match).
* Created at : 09 Aug 2018
* Updated at : 09 Aug 2018
* Xadapter Plugins : https://www.xadapter.com/shop/
* 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
You can’t perform that action at this time.