2015-03-31 154 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(); 
    } 

希望这有助于

相关问题