本文介绍了如何获得WooCommerce运送方式的费用和设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取所有送货方式,以及其价格和标题.但是,当我使用 WC()-> shipping-> get_shipping_methods()时,它将仅返回空的rate数组和空的标题字符串.这是转储:

I'm trying to get all shipping methods, with their rates and title. But when i use WC()->shipping->get_shipping_methods() it will only return empty rates array, and empty title string. Here is a dump:

     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)

我试图通过Google搜索该问题,但没有任何帮助.有谁知道可能是什么问题?

I tried to Google the issue, but nothing really helped.Does anyone know what could be the issue?

感谢一堆!

推荐答案

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

Because WC()->shipping->get_shipping_methods() doesn't load the shipping methods that are set on each shipping zone, it just loads all available shipping methods that you can set in any shipping zone, with all default values and available fields for settings…

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

So as shipping methods rates are different for each shipping zone you need to get first, one or all shipping Zones that are set in shipping section settings.

然后在送货区域中,您可以获取为其设置的所有送货方式费率,例如:

Then from a shipping zone you can get all shipping methods rates that is set for it like:

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

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

This time, as you will see, you get all settings (custom label, costs and other settings) for each shipping method rate set in a shipping zone.

这篇关于如何获得WooCommerce运送方式的费用和设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 18:59