Bạn cần tùy chỉnh lại trang quản lý đơn hàng (order), lịch sử mua hàng.
Bạn cần lấy thông tin sản phẩm (product_id, variation_id, quantity, subtotal, total…) trong đơn hàng $order.
Bạn cần lấy thông tin order billing ( first_name, last_name, email, billing ward, district, city )
Hằng ngày bạn thường lặp lại các thao tác tìm kiếm với các từ khóa: “How to get product info in $order?”, hoặc How to get (customer ID, billing info, order status, payment method, order dates) in $order?”, “How to get order by order_id?”, Hi vọng bài viết này sẽ tập hợp đủ các hàm liên quan đến đơn hàng(order) để giúp bạn tiết kiệm nhiều thời gian!

1. Bạn muốn lấy thông tin từ biến $order

$order_data = array(
    'id'                        => $order->get_id(),
    'order_number'              => $order->get_order_number(),
    'created_at'                => $order->get_date_created() ? $order->get_date_created()->getTimestamp() : 0, // API gives UTC times.
    'updated_at'                => $order->get_date_modified() ? $order->get_date_modified()->getTimestamp() : 0, // API gives UTC times.
    'completed_at'              => $order->get_date_completed() ? $order->get_date_completed()->getTimestamp() : 0, // API gives UTC times.
    'paid_at'                   => $order->get_date_paid() ? $order->get_date_paid()->getTimestamp() : 0, // API gives UTC times.
    'status'                    => $order->get_status(),
    'currency'                  => $order->get_currency(),
    'total'                     => wc_format_decimal( $order->get_total(), 0 ), // vd: 2, là làm tròn đến 2 số thập phân
    'subtotal'                  => wc_format_decimal( $order->get_subtotal(), 0 ),
    'total_line_items_quantity' => $order->get_item_count(), // số lượng sp kh đã mua
    'total_tax'                 => wc_format_decimal( $order->get_total_tax(), 0 ),
    'total_shipping'            => wc_format_decimal( $order->get_shipping_total(), 0 ),
    'cart_tax'                  => wc_format_decimal( $order->get_cart_tax(), 0 ),
    'shipping_tax'              => wc_format_decimal( $order->get_shipping_tax(), 0 ),
    'total_discount'            => wc_format_decimal( $order->get_total_discount(), 0 ),
    'shipping_methods'          => $order->get_shipping_method(),
    'payment_details' => array(
        'method_id'    => $order->get_payment_method(),
        'method_title' => $order->get_payment_method_title(),
        'paid'         => ! is_null( $order->get_date_paid() ),
    ),
    'billing_address' => array(
        'first_name' => $order->get_billing_first_name(),
        'last_name'  => $order->get_billing_last_name(),
        'company'    => $order->get_billing_company(),
        'address_1'  => $order->get_billing_address_1(),
        'address_2'  => $order->get_billing_address_2(),
        'city'       => $order->get_billing_city(),
        'state'      => $order->get_billing_state(),
        'postcode'   => $order->get_billing_postcode(),
        'country'    => $order->get_billing_country(),
        'email'      => $order->get_billing_email(),
        'phone'      => $order->get_billing_phone(),
    ),
    'shipping_address' => array(
        'first_name' => $order->get_shipping_first_name(),
        'last_name'  => $order->get_shipping_last_name(),
        'company'    => $order->get_shipping_company(),
        'address_1'  => $order->get_shipping_address_1(),
        'address_2'  => $order->get_shipping_address_2(),
        'city'       => $order->get_shipping_city(),
        'state'      => $order->get_shipping_state(),
        'postcode'   => $order->get_shipping_postcode(),
        'country'    => $order->get_shipping_country(),
    ),
    'note'                      => $order->get_customer_note(),
    'customer_ip'               => $order->get_customer_ip_address(),
    'customer_user_agent'       => $order->get_customer_user_agent(),
    'customer_id'               => $order->get_user_id(),
    'view_order_url'            => $order->get_view_order_url(),
    'line_items'                => array(),
    'shipping_lines'            => array(),
    'tax_lines'                 => array(),
    'fee_lines'                 => array(),
    'coupon_lines'              => array(),
);   

// add line items: query lấy danh sách sản phẩm trong đơn hàng
foreach ( $order->get_items() as $item_id => $item ) {
    $product    = $item->get_product();
    $item_meta  = $item->get_all_formatted_meta_data();
    $allmeta = $item->get_meta_data(); // show tât cả các thuộc của sản phẩm
    // $somemeta = $item->get_meta( '_whatever', true ); // lấy thông tin thuộc tính của sản phẩm, change _whatever to your product attribute or product_meta
    
    $image_id      = is_object( $product ) ? $product->get_image_id() : 0;       

    $order_data['line_items'][] = array(
        'id'           => $item_id,
        'subtotal'     => wc_format_decimal( $order->get_line_subtotal( $item, false, false ), 0 ),
        'subtotal_tax' => wc_format_decimal( $item->get_subtotal_tax(), 0 ),
        'total'        => wc_format_decimal( $order->get_line_total( $item, false, false ), 0 ),
        'total_tax'    => wc_format_decimal( $item->get_total_tax(), 0 ),
        'price'        => wc_format_decimal( $order->get_item_total( $item, false, false ), 0 ),
        'quantity'     => $item->get_quantity(),
        'tax_class'    => $item->get_tax_class(),
        'name'         => $item->get_name(),
        'product_id'   => $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id(),
        'sku'          => is_object( $product ) ? $product->get_sku() : null,
        'image'        => array(
            'id'  => $image_id,
            'src' => $image_id ? wp_get_attachment_image_url( $image_id, 'full' ) : '',
        )
    );
}

// add shipping, lấy thông tin giao hàng
foreach ( $order->get_shipping_methods() as $shipping_item_id => $shipping_item ) {
    $order_data['shipping_lines'][] = array(
        'id'           => $shipping_item_id,
        'method_id'    => $shipping_item->get_method_id(),
        'method_title' => $shipping_item->get_name(),
        'total'        => wc_format_decimal( $shipping_item->get_total(), 0 ),
    );
}

// add taxes, lấy thông tin thuế
foreach ( $order->get_tax_totals() as $tax_code => $tax ) {
    $order_data['tax_lines'][] = array(
        'id'       => $tax->id,
        'rate_id'  => $tax->rate_id,
        'code'     => $tax_code,
        'title'    => $tax->label,
        'total'    => wc_format_decimal( $tax->amount, 0 ),
        'compound' => (bool) $tax->is_compound,
    );
}

// add fees, lấy thông tin phí
foreach ( $order->get_fees() as $fee_item_id => $fee_item ) {
    $order_data['fee_lines'][] = array(
        'id'        => $fee_item_id,
        'title'     => $fee_item->get_name(),
        'tax_class' => $fee_item->get_tax_class(),
        'total'     => wc_format_decimal( $order->get_line_total( $fee_item ), 0 ),
        'total_tax' => wc_format_decimal( $order->get_line_tax( $fee_item ), 0 ),
    );
}

// add coupons, lấy thông tin mã giảm giá
foreach ( $order->get_items( 'coupon' ) as $coupon_item_id => $coupon_item ) {
    $order_data['coupon_lines'][] = array(
        'id'     => $coupon_item_id,
        'code'   => $coupon_item->get_code(),
        'amount' => wc_format_decimal( $coupon_item->get_discount(), 0 ),
    );
}

echo "<pre>";
    var_dump( $order_data );
echo "</pre>";

 

2. Bạn có mã đơn hàng $order_id  và bạn cần lấy thông tin đơn hàng $order

Trước tiên bạn cần lấy được thông tin đơn hàng $order bằng hàm wc_get_order sau đó bạn có thể truy xuất thông tin theo các bước ở trên

<?php
// Get $order from order ID: Lấy thông tin đơn hàng từ giá trị order ID
  
$order = wc_get_order( $order_id );
  
// Bây giờ bạn có thể lấy các thông tin cần thiết(xem phần bên trên)...
  
if ( $order ) {
   $order->get_id(),
   // etc.
}

3. Lấy tất cả đơn hàng theo $user_id hoặc theo người dùng (user) đang đăng nhập

// Get orders by $user_id
$user_id = get_current_user_id();
$args = array(
  'customer' => $user_id,
   'limit' => -1,
);
$orders = wc_get_orders( $args );

foreach ($orders as $order){
   $order->get_id();
   // etc...
}

Bạn có thể tham khảm chi tiết hơn về các tham số và ví dụ của hàm wc_get_orders tại đây

Lấy tất cả đơn hàng theo email của user mua hàng không đăng nhập

$user_email = 'findorderbyemail@gmail.com';
$args = array(
    'limit' => -1,
    'customer_id' => 0,
    'billing_email' => $user_email            
);

//get all order of guest buy width email
$orders = wc_get_orders( $args );

Bài toán: Sau một thời gian mua hàng, khách hàng với email ở trên đăng ký tài khoản, và bạn muốn tất cả các đơn hàng trước đó của khách hàng này gán lại vào tài khoản khách hàng vừa mới đăng ký. Để giải bài toán trên bạn hãy làm theo các bước sau:

// lấy email của khách hàng đang login hiện tại, hoặc bạn cũng có thể thay thế bằng 1 user id khác
$current_user   = wp_get_current_user();
$user_id        = $current_user->ID; 
$user_email     = $current_user->user_email;

// lấy tất cả đơn hàng khi mua hàng không đặng nhập và có email trùng với email của khách hàng đang đăng nhập
$args = array(
    'limit' => -1,
    'customer_id' => 0,
    'billing_email' => $user_email            
);
$orders = wc_get_orders( $args );

// kiểm tra nếu có đơn hàng thì gán cho khách hàng hiện tại
if( $orders && !is_wp_error( $orders ) ){
    foreach ($orders as $order) {

        if ($order instanceof WC_Order) {
            $billing_email = $order->get_billing_email();
    
            if ( $billing_email === $user_email ) {
                $order->set_customer_id($user_id);
                $order->save();
            }
        }            
    }  
}

Bạn cũng có thể tham khảo thêm các bài viết hướng dẫn hay liên quan đến phần đơn hàng:
https://www.businessbloomer.com/tag/order-status/#colophon

Bài viết liên quan

post-no-image

Cách lấy Page ID một số trang mặc định trong WooCommerce: shop, cart, checkout…

post-no-image

Sắp xếp bài viết theo ID cho sẵn trong WooCommerce

post-no-image

Hướng dẫn sắp xếp lại các field trong trang thanh toán woocommerce (checkout woocommerce)

post-no-image

Hướng dẫn tắt tính năng mua hàng nhưng vẫn hiển thị giá trong WooCommerce