繁体   English   中英

WooCommerce限制基于州的运输方式

[英]WooCommerce limit shipping method based on state

在我们的商店,我们有UPS和USPS运输方式。
我们销售的产品具有各种不同的形状和尺寸,因此我们必须使用API​​,而不是表速率选项。

我只希望当用户从AK,HI,PR,GU,AS,VI或UM输入邮政编码时显示USPS费率。 我已经尝试过有条件的运费和付款方式,但它似乎不起作用。 如果我没错,那只会对结账时退回的价格/选项有效,而不是我需要的购物车。

我已经浏览了许多不同的网页,在这里询问了这个问题,并尝试编辑functions.php文件以匹配响应,但所有答案似乎都已过时。

我能找到的最接近的答案是在下面的链接中选项#2,但是当我尝试它时它不起作用: http//support.wooforce.com/hc/en-us/articles/207515625-Hiding-disabling -a航运服务

这是我从该链接定制的代码,我试图使其工作。 请注意,除了阿拉斯加,夏威夷,波多黎各,关岛,美属萨摩亚,维尔京群岛和次要岛屿之外,我想排除每个州的方法。

任何帮助表示赞赏!

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

function wf_hide_undesired_service_for_required_states($rates, $package)
{
    $exclude = array(
        'wf_shipping_usps:D_PRIORITY_MAIL' => array(
         'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY'
        )
    );
    if (is_array($exclude)) {
        foreach($exclude as $shipping_method => $excluded_states) {
            if (in_array(WC()->customer->shipping_state, $excluded_states)) {
                unset($rates[$shipping_method]);
            }
        }
    }

    return $rates;
}

根据我的理解,您只想在客户选择状态AK,HI,PR,GU,AS,VI或UM时显示USPS实时费率。

这里详细解释了解决方案。 http://www.xadapter.com/2016/06/17/woocommerce-filter-shipping-methods-based-on-state/

下面的代码片段将不使用WooForce USPS Shipping Plugin的任何更改。 如果您正在使用任何其他供应商的WooCommerce USPS插件,您可以通过将变量$eligible_services_for_states_list的元素更改为与该插件相关的适当值来使其工作。

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

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM