【问题标题】:WooCommerce limit shipping method based on stateWooCommerce 限制基于状态的运输方式
【发布时间】:2016-05-31 15:26:27
【问题描述】:

在我们的商店中,我们有 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-shipping-service

这是我从该链接自定义的代码,并且我已尝试使其工作。请注意,我想排除除阿拉斯加、夏威夷、波多黎各、关岛、美属萨摩亚、维尔京群岛和小岛以外的每个州的方法。

感谢任何帮助!

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

【问题讨论】:

    标签: php wordpress woocommerce states usps


    【解决方案1】:

    据我了解,您希望仅在客户选择州 AK、HI、PR、GU、AS、VI 或 UM 时显示 USPS 实时费率。

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

    下面的代码 sn-p 无需对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;
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-13
      • 2018-04-27
      • 1970-01-01
      • 2021-02-11
      • 2019-02-16
      • 2021-02-27
      • 2021-06-07
      • 1970-01-01
      相关资源
      最近更新 更多