问题描述
我显示了有关免费送货所需金额的信息。
赞:
再加10 $即可免费送货
一切正常,并且基于免费送货的固定金额。 我的问题是,即使优惠券设置免费送货,它也会显示。 有什么方法可以检查订单是否已经使用任何免费送货方式?
我在这里找到了一些东西:https://stackoverflow.com/a/32932933/1788961
global $woocommerce;
$shipping_methods = $woocommerce->shipping->load_shipping_methods();
if($shipping_methods['free_shipping']->enabled == "yes")
但这没有帮助。
我还在这里找到了一个有趣的代码段:https://www.businessbloomer.com/woocommerce-hide-shipping-options-free-shipping-available/
$all_free_rates = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$all_free_rates[ $rate_id ] = $rate;
break;
}
}
if ( empty( $all_free_rates )) {
return $rates;
} else {
return $all_free_rates;
}
}
解决方法
我想我已经解决了:
list
然后您可以像这样使用它:
from bisect import bisect
from itertools import accumulate
l = ['example','to','a','list','of','words']
lengths = [*accumulate(map(len,l))]
print(l[bisect(lengths,10)])