【问题标题】:WooCommerce get order shipping address as separate fieldsWooCommerce 将订单送货地址作为单独的字段获取
【发布时间】:2015-06-05 09:29:49
【问题描述】:

我正在构建一个 WooCommerce 插件,它将订单发送到 3rd 方 api。

但是,我发现自 WooCommerce v2.3 以来,无法获取订单送货地址的各个字段/属性。唯一可用的功能是

get_formatted_shipping_address()

它返回一个字符串,似乎返回地址数组“get_shipping_address()”的函数在 2.3 版中已被弃用。

有谁知道将送货地址作为订单数组获取的方法吗?我真的不想求助于使用旧版本的 WooCommerce。也许有一个钩子、动作或类覆盖我可以用来实现这一点?

【问题讨论】:

  • 我或乔希的回答有帮助吗?如果可以,你能接受吗?如果没有,您是否提出了自己的解决方案?如果你做得很好,你应该把它写在这里让其他人找到。最好的,蒂姆

标签: php wordpress woocommerce


【解决方案1】:

看一下'WC_Api_Orders'类的'get_order'函数

        '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,
        ),

您可以直接访问订单的属性。

【讨论】:

    【解决方案2】:

    我以这种简单的方式获得了送货地址:

    function get_shipping_zone(){     
    
    global $woocommerce;
    $post_code = $woocommerce->customer->get_shipping_postcode();
    $zone_postcode = $woocommerce->customer->get_shipping_postcode();
    $zone_city =get_shipping_city(); 
    $zone_state = get_shipping_state(); 
    
    }
    

    您还可以为“$woocommerce->customer”打印_r,您将获得您可能需要的所有元数据,了解它非常有用。

    【讨论】:

      【解决方案3】:

      我会查看这两个公共函数的 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();
          }
      

      希望这会有所帮助,

      提姆

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-12
        • 2014-08-30
        • 2018-06-18
        • 1970-01-01
        • 2021-06-06
        • 2016-07-26
        • 1970-01-01
        相关资源
        最近更新 更多