gpt4 book ai didi

php - 根据州删除特定的 WooCommerce 运输方式

转载 作者:行者123 更新时间:2023-12-02 20:47:48 25 4
gpt4 key购买 nike

我发现下面的代码看起来应该可以实现我想要的功能。但经测试,它不起作用。

我们需要在某些州取消第二天的空运。例如,我们位于宾夕法尼亚州,如果有人从那里购买,我们不希望显示该选项。

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;
}

如何使此代码能够根据州取消设置特定的运输方式?

编辑:使用 Aztec 的答案,我做了一个 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 是什么。如果我将代码放入文本编辑器中,它会注释掉代码。

最佳答案

相反,您应该直接使用 Hook 函数中提供的 $package 参数来以这种方式获取客户目标状态:

add_filter( 'woocommerce_package_rates', 'shipping_rates_for_specific_states', 10, 2 );
function shipping_rates_for_specific_states( $rates, $package ) {

if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

# 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'
);

$destination_state = $package['destination']['state'];

if( in_array( $destination_state, $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'] );
}*/

return $rates;
}

此代码位于事件子主题(或主题)的 function.php 文件中或任何插件文件中。

此代码在 WooCommerce 2.6.x 到 3.0+ 上进行了测试,应该适用于您的自定义费率。

You will need to refresh shipping cached data: disable, save and enable, save related shipping methods for the current shipping zone, in woocommerce shipping settings.


相关答案:Tiered Shipping with multiple Shipping rates options and prices

关于php - 根据州删除特定的 WooCommerce 运输方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43574300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com