Woocommerce order code

//add your order id value here
$order_id = 105;

// Get an instance of the WC_Order Object using the wc_get_order function
// assuming here that we only have the order id in a variable,
// in some cases the order object will be passed directly to an action or filter
$order = wc_get_order( $order_id );

// Get the customer or user id from the order object
$customer_id = $order->get_customer_id();

//this should return exactly the same number as the code above
$user_id = $order->get_user_id();

// Get the WP_User Object instance object
$user = $order->get_user();

// Get the WP_User roles
$user_roles = $user->roles;

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

//note that by default WooCommerce does not collect email and phone number for the shipping address
//so these fields are only available on the billing address
$billing_email  = $order->get_billing_email();
$billing_phone  = $order->get_billing_phone();

$billing_display_data = Array("First Name" => $billing_first_name,
    "Last Name" => $billing_last_name,
    "Company" => $billing_company,
    "Address Line 1" => $billing_address_1,
    "Address Line 2" => $billing_address_2,
    "City" => $billing_city,
    "State" => $billing_state,
    "Post Code" => $billing_postcode,
    "Country" => $billing_country,
    "Email" => $billing_email,
    "Phone" => $billing_phone);

// Customer shipping information details
$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();

$shipping_display_data = Array("First Name" => $billing_first_name,
    "Last Name" => $shipping_last_name,
    "Company" => $shipping_company,
    "Address Line 1" => $shipping_address_1,
    "Address Line 2" => $shipping_address_2,
    "City" => $shipping_city,
    "State" => $shipping_state,
    "Post Code" => $shipping_postcode,
    "Country" => $shipping_country);

echo "Retrieving user information from order id - " . $order_id ."<br><br>";
echo "Customer id from order is " . $customer_id . " the user id is " . $user_id . ", these values should be exactly the same<br><br>";

echo "Here is the customer's billing address from the order - <br>";
foreach ( $billing_display_data as $key => $value ) {
    echo $key . ' - ' . $value . "<br>";
}
echo "<br>";

echo "Here is the customer's shipping address from the order - <br>";
foreach ( $shipping_display_data as $key => $value ) {
    echo $key . ' - ' . $value . "<br>";
}
echo "<br>";

echo "The customer is a member of the following roles - <br>";

foreach ( $user_roles as $value ) {
    echo $value . "<br>";
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *