我遇到了下面的代码,看起来它应该做我想要的。经过测试,但是,它不工作。
我们需要在某些州取消第二天空运。例如,我们位于宾夕法尼亚州,如果有人从那里购买,我们不希望显示该选项。
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($rates),因为它不工作,得到:
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是什么。如果我把它放在文本编辑器中,它会注解掉代码。
3条答案
按热度按时间bxpogfeg1#
相反,您应该直接使用挂钩函数中提供的**
$package
**参数,以如下方式获取客户目标状态:此代码在WooCommerce上从2.6.x到3.0+进行测试,并应与您的自定义费率一起工作。
相关答案:Tiered Shipping with multiple Shipping rates options and prices
a7qyws3x2#
使用此代码,您只能运送选定的状态。
mtb9vblg3#
got from here