根据状态删除特定的WooCommerce运输方法

时间:2017-04-23 17:27:07

标签: php wordpress woocommerce checkout shipping

我遇到了下面的代码,看起来它应该做我想要的。但是,经过测试,它无法正常工作。

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

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 个答案:

答案 0 :(得分:2)

相反,您应该直接使用挂钩函数中提供的 $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

答案 1 :(得分: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 );

使用此代码,您只能发送选定的状态。

答案 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