How to get WooCommerce order details

前端 未结 4 484
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 06:08

In WooCommerce from the following line code:

$order = new WC_Order( $order_id );

How can I get WooCommerce order details from the order id

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 07:06

    Accessing direct properties and related are explained

    // Get an instance of the WC_Order object
                $order = wc_get_order($order_id);
                $order_data = array(
                        'order_id' => $order->get_id(),
                        'order_number' => $order->get_order_number(),
                        'order_date' => date('Y-m-d H:i:s', strtotime(get_post($order->get_id())->post_date)),
                        'status' => $order->get_status(),
                        'shipping_total' => $order->get_total_shipping(),
                        'shipping_tax_total' => wc_format_decimal($order->get_shipping_tax(), 2),
                        'fee_total' => wc_format_decimal($fee_total, 2),
                        'fee_tax_total' => wc_format_decimal($fee_tax_total, 2),
                        'tax_total' => wc_format_decimal($order->get_total_tax(), 2),
                        'cart_discount' => (defined('WC_VERSION') && (WC_VERSION >= 2.3)) ? wc_format_decimal($order->get_total_discount(), 2) : wc_format_decimal($order->get_cart_discount(), 2),
                        'order_discount' => (defined('WC_VERSION') && (WC_VERSION >= 2.3)) ? wc_format_decimal($order->get_total_discount(), 2) : wc_format_decimal($order->get_order_discount(), 2),
                        'discount_total' => wc_format_decimal($order->get_total_discount(), 2),
                        'order_total' => wc_format_decimal($order->get_total(), 2),
                        'order_currency' => $order->get_currency(),
                        'payment_method' => $order->get_payment_method(),
                        'shipping_method' => $order->get_shipping_method(),
                        'customer_id' => $order->get_user_id(),
                        'customer_user' => $order->get_user_id(),
                        'customer_email' => ($a = get_userdata($order->get_user_id() )) ? $a->user_email : '',
                        'billing_first_name' => $order->get_billing_first_name(),
                        'billing_last_name' => $order->get_billing_last_name(),
                        'billing_company' => $order->get_billing_company(),
                        'billing_email' => $order->get_billing_email(),
                        'billing_phone' => $order->get_billing_phone(),
                        'billing_address_1' => $order->get_billing_address_1(),
                        'billing_address_2' => $order->get_billing_address_2(),
                        'billing_postcode' => $order->get_billing_postcode(),
                        'billing_city' => $order->get_billing_city(),
                        'billing_state' => $order->get_billing_state(),
                        'billing_country' => $order->get_billing_country(),
                        'shipping_first_name' => $order->get_shipping_first_name(),
                        'shipping_last_name' => $order->get_shipping_last_name(),
                        'shipping_company' => $order->get_shipping_company(),
                        'shipping_address_1' => $order->get_shipping_address_1(),
                        'shipping_address_2' => $order->get_shipping_address_2(),
                        'shipping_postcode' => $order->get_shipping_postcode(),
                        'shipping_city' => $order->get_shipping_city(),
                        'shipping_state' => $order->get_shipping_state(),
                        'shipping_country' => $order->get_shipping_country(),
                        'customer_note' => $order->get_customer_note(),
                        'download_permissions' => $order->is_download_permitted() ? $order->is_download_permitted() : 0,
                );
    

    Additional details

      $line_items_shipping = $order->get_items('shipping');
                foreach ($line_items_shipping as $item_id => $item) {
                    if (is_object($item)) {
                        if ($meta_data = $item->get_formatted_meta_data('')) :
                            foreach ($meta_data as $meta_id => $meta) :
                                if (in_array($meta->key, $line_items_shipping)) {
                                    continue;
                                }
                                // html entity decode is not working preoperly
                                $shipping_items[] = implode('|', array('item:' . wp_kses_post($meta->display_key), 'value:' . str_replace('×', 'X', strip_tags($meta->display_value))));
                            endforeach;
                        endif;
                    }
                }
    
                //get fee and total
                $fee_total = 0;
                $fee_tax_total = 0;
    
                foreach ($order->get_fees() as $fee_id => $fee) {
    
                    $fee_items[] = implode('|', array(
                            'name:' .  html_entity_decode($fee['name'], ENT_NOQUOTES, 'UTF-8'),
                            'total:' . wc_format_decimal($fee['line_total'], 2),
                            'tax:' . wc_format_decimal($fee['line_tax'], 2),
                    ));
    
                    $fee_total += $fee['line_total'];
                    $fee_tax_total += $fee['line_tax'];
                }
    
                // get tax items
                foreach ($order->get_tax_totals() as $tax_code => $tax) {            
                    $tax_items[] = implode('|', array(
                        'rate_id:'.$tax->id,
                        'code:' . $tax_code,
                        'total:' . wc_format_decimal($tax->amount, 2),
                        'label:'.$tax->label,                
                        'tax_rate_compound:'.$tax->is_compound,
                    ));
                }
    
                // add coupons
                foreach ($order->get_items('coupon') as $_ => $coupon_item) {
    
                    $coupon = new WC_Coupon($coupon_item['name']);
    
                    $coupon_post = get_post((WC()->version < '2.7.0') ? $coupon->id : $coupon->get_id());
                    $discount_amount = !empty($coupon_item['discount_amount']) ? $coupon_item['discount_amount'] : 0;
                    $coupon_items[] = implode('|', array(
                            'code:' . $coupon_item['name'],
                            'description:' . ( is_object($coupon_post) ? $coupon_post->post_excerpt : '' ),
                            'amount:' . wc_format_decimal($discount_amount, 2),
                    ));
                }
    
                foreach ($order->get_refunds() as $refunded_items){
                    $refund_items[] = implode('|', array(
                        'amount:' . $refunded_items->get_amount(),
                'reason:' . $refunded_items->get_reason(),
                        'date:'. date('Y-m-d H-i-s',strtotime((WC()->version < '2.7.0') ? $refunded_items->date_created : $refunded_items->get_date_created())),
                    ));
                }
    

提交回复
热议问题