2015-03-31 78 views
2

我正在構建一個向第三方API發送訂單的WooCommerce插件。WooCommerce作爲單獨的字段獲取訂單送貨地址

但是,我發現自WooCommerce v2.3以後,沒有辦法獲得訂單送貨地址的單個字段/屬性。唯一可用的功能是

get_formatted_shipping_address()

返回一個字符串,它似乎返回地址陣列「get_shipping_address()」已在2.3版本中被棄用的功能。

有沒有人知道一種方式來獲取訂單數組的地址?我真的不想訴諸使用舊版本的WooCommerce。也許有一個鉤子,行動或類覆蓋我可以用來實現這一目標?

+0

做了我的或喬希的回答幫助?如果是這樣,你能接受嗎?如果沒有,你是否想出了自己的解決方案?如果你做得很好,你應該把它寫在這裏供其他人找到。最好,蒂姆 – mpactMEDIA 2016-03-23 00:09:56

回答

3

我在這個簡單的方式送貨地址:

function get_shipping_zone(){  

global $woocommerce; 
$customer = new WC_Customer(); 
$post_code = $woocommerce->customer->get_shipping_postcode(); 
$zone_postcode = $woocommerce->customer->get_shipping_postcode(); 
$zone_city =get_shipping_city(); 
$zone_state = get_shipping_state(); 

} 

你可以也爲「$ wooc」的print_r ommerce-> customer「,您將獲得您可能需要的所有元數據,瞭解它非常有用。

4

看一看類的WC_Api_Orders「

 'shipping_address' => array(
      'first_name' => $order->shipping_first_name, 
      'last_name' => $order->shipping_last_name, 
      'company' => $order->shipping_company, 
      'address_1' => $order->shipping_address_1, 
      'address_2' => $order->shipping_address_2, 
      'city'  => $order->shipping_city, 
      'state'  => $order->shipping_state, 
      'postcode' => $order->shipping_postcode, 
      'country' => $order->shipping_country, 
     ), 

您只需直接訪問順序的屬性的「get_order」功能。

1

我會看看WC_Abstract_Order這兩個公共職能。

/** 
    * Get a formatted shipping address for the order. 
    * 
    * @return string 
    */ 
    public function get_formatted_shipping_address() { 
     if (! $this->formatted_shipping_address) { 

      if ($this->shipping_address_1 || $this->shipping_address_2) { 

       // Formatted Addresses 
       $address = apply_filters('woocommerce_order_formatted_shipping_address', array(
        'first_name' => $this->shipping_first_name, 
        'last_name'  => $this->shipping_last_name, 
        'company'  => $this->shipping_company, 
        'address_1'  => $this->shipping_address_1, 
        'address_2'  => $this->shipping_address_2, 
        'city'   => $this->shipping_city, 
        'state'   => $this->shipping_state, 
        'postcode'  => $this->shipping_postcode, 
        'country'  => $this->shipping_country 
       ), $this); 

       $this->formatted_shipping_address = WC()->countries->get_formatted_address($address); 
      } 
     } 

     return $this->formatted_shipping_address; 
    } 

而且......

/** 
    * Calculate shipping total. 
    * 
    * @since 2.2 
    * @return float 
    */ 
    public function calculate_shipping() { 

     $shipping_total = 0; 

     foreach ($this->get_shipping_methods() as $shipping) { 
      $shipping_total += $shipping['cost']; 
     } 

     $this->set_total($shipping_total, 'shipping'); 

     return $this->get_total_shipping(); 
    } 

希望這有助於

相關問題