Php 如何获取商业运输方式成本和设置?

Php 如何获取商业运输方式成本和设置?,php,wordpress,woocommerce,location,shipping-method,Php,Wordpress,Woocommerce,Location,Shipping Method,我试图得到所有的运输方式,以及它们的价格和标题。但是当我使用WC()->shipping->get\u shipping\u methods()时,它只返回空的rates数组和空的title字符串。这里是一个垃圾场: array (size=3) 'flat_rate' => object(WC_Shipping_Flat_Rate)[1861] protected 'fee_cost' => string '' (length=0)

我试图得到所有的运输方式,以及它们的价格和标题。但是当我使用
WC()->shipping->get\u shipping\u methods()
时,它只返回空的rates数组和空的title字符串。这里是一个垃圾场:

     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)
我试着用谷歌搜索这个问题,但没有任何帮助。 有人知道可能是什么问题吗

非常感谢

因为
WC()->shipping->get_shipping_methods()
不会加载在每个装运区上设置的装运方法,它只加载您可以在任何装运区中设置的所有可用装运方法,以及所有默认值和可用的设置字段

请记住,配送方式成本和设置是按配送区域设置的,并且与地点(地区、国家、州或邮政编码)相关

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

然后,您可以从配送区获得为其设置的所有配送方式费率,如:

// Get all your existing shipping zones IDS
$zone_ids = array_keys( array('') + WC_Shipping_Zones::get_zones() );

// Loop through shipping Zones IDs
foreach ( $zone_ids as $zone_id ) 
{
    // Get the shipping Zone object
    $shipping_zone = new WC_Shipping_Zone($zone_id);

    // Get all shipping method values for the shipping zone
    $shipping_methods = $shipping_zone->get_shipping_methods( true, 'values' );

    // Loop through each shipping methods set for the current shipping zone
    foreach ( $shipping_methods as $instance_id => $shipping_method ) 
    {
        // The dump of protected data from the current shipping method
        var_dump($shipping_method);
    }
}

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

非常感谢您,先生。你刚刚救了我一周。感谢您花时间给出如此详细和有益的回答。注意安全!