如何获取 WooCommerce 运输方式成本和设置?

Ask*_*rci 2 php wordpress location woocommerce shipping-method

我正在尝试获取所有运输方式及其费率和名称。但是当我使用它时,WC()->shipping->get_shipping_methods()它只会返回空的费率数组和空的标题字符串。这是一个转储:

     array (size=3)
  'flat_rate' => 
    object(WC_Shipping_Flat_Rate)[1861]
      protected 'fee_cost' => string '' (length=0)
      public 'supports' => 
        array (size=3)
          0 => string 'shipping-zones' (length=14)
          1 => string 'instance-settings' (length=17)
          2 => string 'instance-settings-modal' (length=23)
      public 'id' => string 'flat_rate' (length=9)
      public 'method_title' => string 'Flat rate' (length=9)
      public 'method_description' => string 'Lets you charge a fixed rate for shipping.' (length=42)
      public 'enabled' => string 'yes' (length=3)
      public 'title' => string '' (length=0)
      public 'rates' => 
        array (size=0)
          empty
      public 'tax_status' => string '' (length=0)
      public 'fee' => null
      public 'minimum_fee' => null
      public 'instance_id' => int 0
      public 'instance_form_fields' => 
        array (size=3)
          'title' => 
            array (size=5)
              ...
          'tax_status' => 
            array (size=5)
              ...
          'cost' => 
            array (size=7)
              ...
      public 'instance_settings' => 
        array (size=0)
          empty
      public 'availability' => null
      public 'countries' => 
        array (size=0)
          empty
      public 'plugin_id' => string 'woocommerce_' (length=12)
      public 'errors' => 
        array (size=0)
          empty
      public 'settings' => 
        array (size=4)
          'title' => string '' (length=0)
          'tax_status' => string '' (length=0)
          'cost' => string '' (length=0)
          'type' => string 'class' (length=5)
      public 'form_fields' => 
        array (size=0)
          empty
      protected 'data' => 
        array (size=0)
          empty
      public 'cost' => string '' (length=0)
      public 'type' => string 'class' (length=5)
Run Code Online (Sandbox Code Playgroud)

我尝试用谷歌搜索这个问题,但没有任何帮助。有谁知道可能是什么问题?

非常感谢!

Loi*_*tec 6

因为WC()->shipping->get_shipping_methods()不会加载在每个运输区域上设置的运输方式,所以它仅加载您可以在任何运输区域中设置的所有可用运输方式,以及设置的所有默认值和可用字段\xe2\x80\xa6

\n\n
\n

请记住,运输方式成本和设置由运输区域设置,并且与位置(地区、国家、州或邮政编码)相关。

\n
\n\n

因此,由于每个运输区域的运输方式费率不同,您需要首先获取在运输部分设置中设置的一个或所有运输区域。

\n\n

然后,您可以从运输区域获取为其设置的所有运输方式费率,例如:

\n\n
// Get all your existing shipping zones IDS\n$zone_ids = array_keys( array('') + WC_Shipping_Zones::get_zones() );\n\n// Loop through shipping Zones IDs\nforeach ( $zone_ids as $zone_id ) \n{\n    // Get the shipping Zone object\n    $shipping_zone = new WC_Shipping_Zone($zone_id);\n\n    // Get all shipping method values for the shipping zone\n    $shipping_methods = $shipping_zone->get_shipping_methods( true, 'values' );\n\n    // Loop through each shipping methods set for the current shipping zone\n    foreach ( $shipping_methods as $instance_id => $shipping_method ) \n    {\n        // The dump of protected data from the current shipping method\n        var_dump($shipping_method);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这次,正如您将看到的,您将获得运输区域中设置的每种运输方式费率的所有设置(自定义标签、成本和其他设置)。

\n