2017-04-23 119 views
1

我遇到了下面的代码,它看起来应该按照我的意愿去做。但是,经过测试,它不起作用。根据状态删除特定的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是什么。如果我把它放在文本编辑器中,它会注释掉代码。

回答

1

相反,你应该直接使用您的挂钩函数提供的$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+以上进行测试,并应与您的自定义费率。

您将需要刷新航运缓存数据:关闭,保存并启用,保存当前航运区相关的运输方式,在woocommerce送货设置。


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

+0

LoicTheAztec,不幸的是我仍然得到第2天空气,以示对数组中排除的状态。有可能有干扰吗?我似乎无法弄清楚。 谢谢你的时间。 – corporalpoon

+0

我在$ destination_state上做了一个var_dump并得到:string(0)“” 另外,我的客户端正在使用这个改变后的“Easy Post Shipping Plugin”。也许这是干扰?我在$汇率上做了一个var转储。我把它写在我的问题上。 – corporalpoon

+0

它只是输出我有的产品和他们的信息,如描述,创建日期等。运费没有。 – corporalpoon

0
/** 
* @snippet  Only ship to PA WooCommerce 2.1+ 
* @how-to  Watch tutorial @ https://businessbloomer.com/?p=19055 
* @sourcecode https://businessbloomer.com/?p=309 
* @author  Rodolfo Melogli 
* @testedwith WooCommerce 2.6.14 
*/ 

function bbloomer_only_ship_to_pa($rates, $package) { 
global $woocommerce; 
$excluded_states = array('AL','AK','AS','AZ','AR','CA','CO','CT','DE','DC','FL','FM','GA','GU','HI','ID','IL','IN','IA','KS','KY','LA','ME','MH','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','MP','OH','OK','OR','PW','PR','RI','SC','SD','TN','TX','UM','UT','VT','VA','VI','WA','WV','WI','WY'); 
if(in_array(WC()->customer->shipping_state, $excluded_states)) { 
    $rates = array(); 
} 
return $rates; 
} 

add_filter('woocommerce_package_rates', 'bbloomer_only_ship_to_pa', 10, 2); 

与此代码,你可以只船选择的状态。

0
add_filter('woocommerce_package_rates', 'xa_show_shipping_method_based_on_state', 10, 2); 

function xa_show_shipping_method_based_on_state($available_shipping_methods, $package) { 

    $states_list = array('AK', 'HI', 'PR', 'GU', 'AS', 'VI', 'UM'); 

    $eligible_services_for_states_list = array(
      'wf_shipping_usps:flat_rate_box_priority', 
      'wf_shipping_usps:flat_rate_box_express', 
      'wf_shipping_usps:D_FIRST_CLASS', 
      'wf_shipping_usps:D_EXPRESS_MAIL', 
      'wf_shipping_usps:D_STANDARD_POST', 
      'wf_shipping_usps:D_MEDIA_MAIL', 
      'wf_shipping_usps:D_LIBRARY_MAIL', 
      'wf_shipping_usps:D_PRIORITY_MAIL', 
      'wf_shipping_usps:I_EXPRESS_MAIL', 
      'wf_shipping_usps:I_PRIORITY_MAIL', 
      'wf_shipping_usps:I_GLOBAL_EXPRESS', 
      'wf_shipping_usps:I_FIRST_CLASS', 
      'wf_shipping_usps:I_POSTCARDS',   
     ); 

    // Basically, below code will reset shipping services if the shipping 
    // state is other than defined states_list. 
    if (!in_array(WC()->customer->shipping_state, $states_list)) { 
     foreach ($eligible_services_for_states_list as &$value) { 
      unset($available_shipping_methods[$value]);  
     } 
    } 

    return $available_shipping_methods; 
} 

got from here