我一直在做一些研究,但找不到解决方案,所以我自己写了一个代码。它不起作用,结帐仍然计算购物车中产品的所有运费。
function only_show_most_expensive_shipping_rate( $rates, $package ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
$most_expensive_method = '';
if ( is_array( $rates ) ) :
foreach ( $rates as $key => $rate ) :
if ( empty( $most_expensive_method ) || $rate->cost > $most_expensive_method->cost ) :
$most_expensive_method = $rate;
endif;
endforeach;
endif;
// Return the most expensive rate when possible
if ( ! empty( $most_expensive_method ) ) :
return array( $most_expensive_method->id => $most_expensive_method );
endif;
return $rates;
}
add_action( 'woocommerce_package_rates', 'only_show_most_expensive_shipping_rate', 10, 2 );
有人能指出我做错了什么吗?谢谢你