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

thtygnil  于 2023-01-16  发布在  PHP
关注(0)|答案(3)|浏览(123)

我遇到了下面的代码,看起来它应该做我想要的。经过测试,但是,它不工作。
我们需要在某些州取消第二天空运。例如,我们位于宾夕法尼亚州,如果有人从那里购买,我们不希望显示该选项。

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

bxpogfeg

bxpogfeg1#

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

a7qyws3x

a7qyws3x2#

/**
 * @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 );

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

mtb9vblg

mtb9vblg3#

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

相关问题