Код: Выделить всё
add_filter( 'woocommerce_package_rates','deal_with_woocommerce_available_shipping_methods', 10, 2 );
function deal_with_woocommerce_available_shipping_methods( $rates, $package ) {
// if there are no rates don't do anything
if ( ! $rates ) {
return;
}
// get rid of rates if free is there
$rates_arr = array();
$free = false;
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$rates_arr[ $rate_id ] = $rate;
$free=true;
break;
}
}
// if it is free ship return it
if ( !empty( $rates_arr ) ) {
return $rates_arr;
}
/* remove all but ups if longest item > 24 */
if (!$free && longestItemLengthOver24()) {
foreach ( $rates as $rate_id => $rate ) {
if (strpos('UPS', serialize($rate))) {
$rates_arr[ $rate_id ] = $rate;
break;
}
}
}
// get an array of prices
$prices = array();
foreach( $rates_arr as $rate ) {
$prices[] = $rate->cost;
}
// use the prices to sort the rates
array_multisort( $prices, $rates_arr );
// return the rates
return $rates_arr;
}
function longestItemLengthOver24(){
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_length = $cart_item['data']->get_length(); // Get producct length
if( ! empty($product_length) ){
if ($product_length>24) {
return true;
}
}
}
return false;
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... kage-rates
Мобильная версия