Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据州的不同,删除特定的装运方法_Php_Wordpress_Woocommerce_Checkout_Shipping - Fatal编程技术网

Php 根据州的不同,删除特定的装运方法

Php 根据州的不同,删除特定的装运方法,php,wordpress,woocommerce,checkout,shipping,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,

我遇到了下面的代码,看起来它应该做我想要的。但是,经过测试,它不起作用

在某些州,我们需要取消第二天空运。例如,我们位于宾夕法尼亚州,如果有人从那里购买,我们不希望看到这种选择

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_转储($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是什么。如果我把代码放在文本编辑器中,它会注释掉代码

相反,您应该直接使用hooked函数中提供的
$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配送设置中,禁用、保存并启用、保存当前配送区域的相关配送方法


有关答案:

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

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

LoicTheAztec,不幸的是,我仍然有第二天的空气显示阵列中的排除状态。可能有什么干扰吗?我似乎不明白。谢谢你的时间。我在$destination_state上做了一个var_转储,得到了:string(0)“,另外,我的客户端正在使用这个经过修改的“Easy Post Shipping插件”。也许这是干扰?我做了一个美元利率的var转储。我把它放在我的问题里。它只输出我拥有的产品和它们的信息,比如描述、创建日期等等。航运方面没有。
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;
}