當我們進行自定義開發時總會需要用到客戶的訂單資料,這篇文章將會為你提供多種方法取得客戶訂單資料的PHP代碼。
使用WC_Order Class
// 從訂單 ID 取得 WC_Order $order = wc_get_order( $order_id ); // 取得客戶 ID (User ID) $customer_id = $order->get_customer_id(); // Or $order->get_user_id(); // 取得 WP_User 物件 $user = $order->get_user(); // 取得 WP_User 角色 $user_roles = $user->roles; // 客戶帳單詳細資訊 $billing_email = $order->get_billing_email(); $billing_phone = $order->get_billing_phone(); $billing_first_name = $order->get_billing_first_name(); $billing_last_name = $order->get_billing_last_name(); $billing_company = $order->get_billing_company(); $billing_address_1 = $order->get_billing_address_1(); $billing_address_2 = $order->get_billing_address_2(); $billing_city = $order->get_billing_city(); $billing_state = $order->get_billing_state(); $billing_postcode = $order->get_billing_postcode(); $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_city = $order->get_shipping_city(); $shipping_state = $order->get_shipping_state(); $shipping_postcode = $order->get_shipping_postcode(); $shipping_country = $order->get_shipping_country();
使用WC_Order get_data()方式
$data = $order->get_data(); $order_id = $data['id']; $order_parent_id = $data['parent_id']; // 取得客戶 ID (User ID) $customer_id = $data['customer_id']; // 帳單詳細資訊 $billing_email = $data['billing']['email']; $billing_phone = $order_data['billing']['phone']; $billing_first_name = $data['billing']['first_name']; $billing_last_name = $data['billing']['last_name']; $billing_company = $data['billing']['company']; $billing_address_1 = $data['billing']['address_1']; $billing_address_2 = $data['billing']['address_2']; $billing_city = $data['billing']['city']; $billing_state = $data['billing']['state']; $billing_postcode = $data['billing']['postcode']; $billing_country = $data['billing']['country']; //送貨詳細資訊 $shipping_first_name = $data['shipping']['first_name']; $shipping_last_name = $data['shipping']['last_name']; $shipping_company = $data['shipping']['company']; $shipping_address_1 = $data['shipping']['address_1']; $shipping_address_2 = $data['shipping']['address_2']; $shipping_city = $data['shipping']['city']; $shipping_state = $data['shipping']['state']; $shipping_postcode = $data['shipping']['postcode']; $shipping_country = $data['shipping']['country'];
使用WC_Customer Class
$user_id = get_post_meta( $order_id, '_customer_user', true ); $customer = new WC_Customer( $user_id ); $username = $customer->get_username(); $user_email = $customer->get_email(); $first_name = $customer->get_first_name(); $last_name = $customer->get_last_name(); $display_name = $customer->get_display_name(); //帳單詳細資訊 $billing_first_name = $customer->get_billing_first_name(); $billing_last_name = $customer->get_billing_last_name(); $billing_company = $customer->get_billing_company(); $billing_address_1 = $customer->get_billing_address_1(); $billing_address_2 = $customer->get_billing_address_2(); $billing_city = $customer->get_billing_city(); $billing_state = $customer->get_billing_state(); $billing_postcode = $customer->get_billing_postcode(); $billing_country = $customer->get_billing_country(); //送貨詳細資訊 $shipping_first_name = $customer->get_shipping_first_name(); $shipping_last_name = $customer->get_shipping_last_name(); $shipping_company = $customer->get_shipping_company(); $shipping_address_1 = $customer->get_shipping_address_1(); $shipping_address_2 = $customer->get_shipping_address_2(); $shipping_city = $customer->get_shipping_city(); $shipping_state = $customer->get_shipping_state(); $shipping_postcode = $customer->get_shipping_postcode(); $shipping_country = $customer->get_shipping_country();



