首页 文章

WooCommerce将订单送货地址作为单独的字段

提问于
浏览
2

我正在构建一个WooCommerce插件,它将订单发送给第三方API .

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

get_formatted_shipping_address()

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

有没有人知道如何将送货地址作为订单的数组?我真的不想使用旧版本的WooCommerce . 也许有一个钩子,动作或类覆盖我可以用来实现这个?

3 回答

  • 4

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

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

    您还可以print_r为“$ woocommerce-> customer”获取您可能需要的所有元数据,了解它非常有用 .

  • 4

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

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

  • 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();
        }
    

    希望这可以帮助,

    蒂姆

相关问题