Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nczz/9f0df57e1eb13263e6065bbd58614732 to your computer and use it in GitHub Desktop.
Save nczz/9f0df57e1eb13263e6065bbd58614732 to your computer and use it in GitHub Desktop.
Works with WooCommerce 2.6.x or below
<?php
if (!function_exists('getOrderDetailById')) {
function getOrderDetailById($id, $fields = null, $filter = array()) {
if (is_wp_error($id))
return $id;
// Get the decimal precession
$dp = isset($filter['dp']) ? intval($filter['dp']) : 2;
$order = wc_get_order($id);
$order_post = get_post($id);
$order_data = array(
'id' => $order->id,
'order_number' => $order->get_order_number(),
'created_at' => $order_post->post_date,
'updated_at' => $order_post->post_modified,
'completed_at' => !empty($order->completed_date) ? $order->completed_date : '',
'status' => $order->get_status(),
'currency' => $order->get_order_currency(),
'total' => wc_format_decimal($order->get_total(), $dp),
'subtotal' => wc_format_decimal($order->get_subtotal(), $dp),
'total_line_items_quantity' => $order->get_item_count(),
'total_tax' => wc_format_decimal($order->get_total_tax(), $dp),
'total_shipping' => wc_format_decimal($order->get_total_shipping(), $dp),
'cart_tax' => wc_format_decimal($order->get_cart_tax(), $dp),
'shipping_tax' => wc_format_decimal($order->get_shipping_tax(), $dp),
'total_discount' => wc_format_decimal($order->get_total_discount(), $dp),
'shipping_methods' => $order->get_shipping_method(),
'order_key' => $order->order_key,
'payment_details' => array(
'method_id' => $order->payment_method,
'method_title' => $order->payment_method_title,
'paid_at' => !empty($order->paid_date) ? $order->paid_date : '',
),
'billing_address' => array(
'first_name' => $order->billing_first_name,
'last_name' => $order->billing_last_name,
'company' => $order->billing_company,
'address_1' => $order->billing_address_1,
'address_2' => $order->billing_address_2,
'city' => $order->billing_city,
'state' => $order->billing_state,
'formated_state' => WC()->countries->states[$order->billing_country][$order->billing_state], //human readable formated state name
'postcode' => $order->billing_postcode,
'country' => $order->billing_country,
'formated_country' => WC()->countries->countries[$order->billing_country], //human readable formated country name
'email' => $order->billing_email,
'phone' => $order->billing_phone,
),
'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,
'formated_state' => WC()->countries->states[$order->shipping_country][$order->shipping_state], //human readable formated state name
'postcode' => $order->shipping_postcode,
'country' => $order->shipping_country,
'formated_country' => WC()->countries->countries[$order->shipping_country] //human readable formated country name
),
'note' => $order->customer_note,
'customer_ip' => $order->customer_ip_address,
'customer_user_agent' => $order->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(),
);
//getting all line items
foreach ($order->get_items() as $item_id => $item) {
$product = $order->get_product_from_item($item);
$product_id = null;
$product_sku = null;
// Check if the product exists.
if (is_object($product)) {
$product_id = !empty($product->variation_id) ? $product->variation_id : $product->id;
$product_sku = $product->get_sku();
}
$meta = new WC_Order_Item_Meta($item, $product);
$item_meta = array();
$hideprefix = ( isset($filter['all_item_meta']) && $filter['all_item_meta'] === 'true' ) ? null : '_';
foreach ($meta->get_formatted($hideprefix) as $meta_key => $formatted_meta) {
$item_meta[] = array(
'key' => $formatted_meta['key'],
'label' => $formatted_meta['label'],
'value' => $formatted_meta['value'],
);
}
$order_data['line_items'][] = array(
'id' => $item_id,
'subtotal' => wc_format_decimal($order->get_line_subtotal($item, false, false), $dp),
'subtotal_tax' => wc_format_decimal($item['line_subtotal_tax'], $dp),
'total' => wc_format_decimal($order->get_line_total($item, false, false), $dp),
'total_tax' => wc_format_decimal($item['line_tax'], $dp),
'price' => wc_format_decimal($order->get_item_total($item, false, false), $dp),
'quantity' => wc_stock_amount($item['qty']),
'tax_class' => (!empty($item['tax_class']) ) ? $item['tax_class'] : null,
'name' => $item['name'],
'product_id' => !empty($product->variation_id) ? $product->parent_id : $product_id,
'variation_id' => !empty($product->variation_id) ? $product_id : 0,
'sku' => $product_sku,
'product_url' => get_permalink($product_id),
'product_thumbnail_url' => wp_get_attachment_image_src(get_post_thumbnail_id($product_id), 'thumbnail', TRUE)[0],
'meta' => $item_meta,
);
}
//getting shipping
foreach ($order->get_shipping_methods() as $shipping_item_id => $shipping_item) {
$order_data['shipping_lines'][] = array(
'id' => $shipping_item_id,
'method_id' => $shipping_item['method_id'],
'method_title' => $shipping_item['name'],
'total' => wc_format_decimal($shipping_item['cost'], $dp),
);
}
//getting taxes
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, $dp),
'compound' => (bool) $tax->is_compound,
);
}
//getting fees
foreach ($order->get_fees() as $fee_item_id => $fee_item) {
$order_data['fee_lines'][] = array(
'id' => $fee_item_id,
'title' => $fee_item['name'],
'tax_class' => (!empty($fee_item['tax_class']) ) ? $fee_item['tax_class'] : null,
'total' => wc_format_decimal($order->get_line_total($fee_item), $dp),
'total_tax' => wc_format_decimal($order->get_line_tax($fee_item), $dp),
);
}
//getting coupons
foreach ($order->get_items('coupon') as $coupon_item_id => $coupon_item) {
$order_data['coupon_lines'][] = array(
'id' => $coupon_item_id,
'code' => $coupon_item['name'],
'amount' => wc_format_decimal($coupon_item['discount_amount'], $dp),
);
}
return array('order' => apply_filters('woocommerce_api_order_response', $order_data, $order, $fields));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment