我遇到了下面的代码,它看起来应该按照我的意愿去做。但是,经过测试,它不起作用。根据状态删除特定的WooCommerce运输方式
我们需要摆脱某些州的第二天空运。例如,我们位于宾夕法尼亚州,如果有人从那里购买,我们不希望该选项显示。
add_filter('woocommerce_package_rates', 'unset_ups_shipping_rates_for_specific_states' , 10, 2);
function unset_ups_shipping_rates_for_specific_states($rates, $package) {
// Setup an array of states that do not allow UPS Shipping 2nd Day Air. As of 10/18/2015 we added 3 days ground too.
$excluded_states = array('FL','AL','KY','LA','MS','TN','GA','SC','NC','KY','DC','VA','AR','CT','DE','IL','IN','KS','MA','ME','MD','MN','MI','MO','NH','NJ','NY','OH','OK','PA','RI','TX','VT','WV','WI');
foreach (WC()->cart->get_shipping_packages() as $package) {
if(in_array($package, $excluded_states)) :
unset($rates['sups:02:0']); // Unset 2-Day Air
unset($rates['ups:02']);
unset($rates['sups:12:0']); // Unset 3-Day Select too
unset($rates['ups:12']);
else:
unset($rates['sups:03:0']); // Unset Ground
unset($rates['ups:03']);
endif;
}
// Return what's left of the $rates array
return $rates;
}
我该如何使这段代码工作以取消设置基于状态的特定传送方法?
编辑:使用阿兹台克人的答案,我做了的var_dump($率),因为它不工作,并得到:
array(2) {
["UPS-Ground"]=> object(WC_Shipping_Rate)#1486 (6) {
["id"]=> string(10) "UPS-Ground"
["label"]=> string(10) "UPS Ground"
["cost"]=> string(5) "11.60"
["taxes"]=> array(0) { }
["method_id"]=> string(20) "easypostshippingtool"
["meta_data":"WC_Shipping_Rate":private]=> array(0) { }
}
["UPS-2ndDayAir"]=> object(WC_Shipping_Rate)#1514 (6) {
["id"]=> string(13) "UPS-2ndDayAir"
["label"]=> string(13) "UPS 2ndDayAir"
["cost"]=> string(5) "23.31"
["taxes"]=> array(0) { }
["method_id"]=> string(20) "easypostshippingtool"
["meta_data":"WC_Shipping_Rate":private]=> array(0) { }
}
}
你认为easypostshippingtool与溶液的干扰?另外,我不确定#1486是什么。如果我把它放在文本编辑器中,它会注释掉代码。
LoicTheAztec,不幸的是我仍然得到第2天空气,以示对数组中排除的状态。有可能有干扰吗?我似乎无法弄清楚。 谢谢你的时间。 – corporalpoon
我在$ destination_state上做了一个var_dump并得到:string(0)“” 另外,我的客户端正在使用这个改变后的“Easy Post Shipping Plugin”。也许这是干扰?我在$汇率上做了一个var转储。我把它写在我的问题上。 – corporalpoon
它只是输出我有的产品和他们的信息,如描述,创建日期等。运费没有。 – corporalpoon