【问题标题】:Remove specific WooCommerce shipping methods depending on states根据州删除特定的 WooCommerce 运输方式
【发布时间】:2017-09-20 07:42:18
【问题描述】:

我遇到了下面的代码,它看起来应该可以满足我的需求。但是,经过测试,它不起作用。

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

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

【问题讨论】:

    标签: php wordpress woocommerce checkout shipping


    【解决方案1】:

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

    【讨论】:

    • LoicTheAztec,不幸的是,我仍然需要第二天的空气来显示阵列中的排除状态。会不会有什么干扰?我似乎无法弄清楚。感谢您的宝贵时间。
    • 我在 $destination_state 上做了一个 var_dump 并得到:string(0) "" 另外,我的客户正在使用这个修改后的“Easy Post Shipping Plugin”。也许这是干扰?我对 $rates 做了一个 var 转储。我把它放在我的问题中。
    • 它只输出我拥有的产品及其信息,例如描述、创建日期等。没有运费。
    【解决方案2】:
    /**
     * @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 );
    

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

    【讨论】:

      【解决方案3】:
      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-03
        • 2021-03-29
        • 2021-02-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-24
        • 2014-07-05
        • 2014-08-06
        相关资源
        最近更新 更多