Created
December 2, 2021 13:38
-
-
Save nczz/1158443c116694789d153b8986cc3317 to your computer and use it in GitHub Desktop.
WooCommerce 客製化匯出訂單格式的方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: 訂單匯出 | |
* Plugin URI: | |
* Description: 匯出OOO訂單格式 | |
* Version: 1.0.0 | |
* Author: Chun | |
* Author URI: https://www.mxp.tw/contact/ | |
* License: MIT | |
*/ | |
if (!defined('WPINC')) { | |
die; | |
} | |
function mxp_add_submenu_pages() { | |
add_submenu_page('woocommerce', '匯出訂單', '匯出訂單', 'manage_woocommerce', 'mxp-mm-order-export', 'export_orders_page'); | |
} | |
add_action('admin_menu', 'mxp_add_submenu_pages'); | |
function export_orders_page() { | |
wp_register_script('mxp-mm-order-export-custom-js', plugin_dir_url(__FILE__) . 'js/main.js', array('jquery'), '1.0', false); | |
wp_localize_script('mxp-mm-order-export-custom-js', 'mxp_mm_order_export', array( | |
'ajaxurl' => admin_url('admin-ajax.php'), | |
'nonce' => wp_create_nonce('wp_rest'), | |
)); | |
wp_enqueue_script('mxp-mm-order-export-custom-js'); | |
echo '訂單匯出起始日:<input id="start_date" name="start_date" type="date"><br>'; | |
echo '訂單匯出結束日:<input id="end_date" name="end_date" type="date"><br>'; | |
echo '<button type="button" id="go_export_btn">匯出</button>'; | |
} | |
function mxp_export_orders_csv() { | |
include dirname(__FILE__) . '/vendor/autoload.php'; | |
$initial_date = $_GET['start_date']; | |
$final_date = $_GET['end_date']; | |
$posts_data = wc_get_orders(array( | |
'limit' => -1, | |
'type' => 'shop_order', | |
'status' => array('wc-completed'), | |
'date_created' => $initial_date . '...' . $final_date, | |
)); | |
$csv_arr = array(); | |
$csv_arr[] = array('匯出欄位一', '匯出欄位二', '匯出欄位三');//欄位 | |
foreach ($posts_data as $key => $order) { | |
$d = mxp_get_order_detail_by_id($order->ID); | |
// .... 這邊開始處理要從訂單裡取出的欄位資料 | |
// 只需要實作此段對應欄位標頭 | |
} | |
// 準備匯出成 Excel 格式 | |
$filename = "export-" . date("Y-m-d-H-i-s") . ".xlsx"; | |
$writer = new XLSXWriter(); | |
$writer->writeSheet($csv_arr); | |
$buffer = $writer->writeToString(); | |
header('Content-Description: File Transfer'); | |
if (headers_sent()) { | |
$writer->Error('Some data has already been output to browser, can\'t send XLSX file'); | |
} | |
header('Content-Transfer-Encoding: binary'); | |
header('Cache-Control: public, must-revalidate, max-age=0'); | |
header('Pragma: public'); | |
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); | |
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); | |
header('Content-Type: application/force-download'); | |
header('Content-Type: application/octet-stream', false); | |
header('Content-Type: application/download', false); | |
header('Content-Type: application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', false); | |
if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) { | |
// don't use length if server using compression | |
header('Content-Length: ' . strlen($buffer)); | |
} | |
header('Content-disposition: attachment; filename="' . $filename . '"'); | |
print $buffer; | |
exit; | |
} | |
add_action('wp_ajax_mxp_export_orders_csv', 'mxp_export_orders_csv'); | |
add_action('wp_ajax_nopriv_mxp_export_orders_csv', 'mxp_export_orders_csv'); | |
function mxp_csvstr(array $fields): string{ | |
$f = fopen('php://memory', 'r+'); | |
if (fputcsv($f, $fields) === false) { | |
return false; | |
} | |
rewind($f); | |
$csv_line = stream_get_contents($f); | |
return rtrim($csv_line); | |
} | |
//取得訂單細節資訊的方法 | |
function mxp_get_order_detail_by_id($id, $fields = null, $filter = array()) { | |
if (is_wp_error($id)) { | |
return false; | |
} | |
// Get the decimal precession | |
$dp = (isset($filter['dp'])) ? intval($filter['dp']) : 2; | |
$order = wc_get_order($id); //getting order Object | |
if ($order === false) { | |
return false; | |
} | |
$order_data = array( | |
'id' => $order->get_id(), | |
'order_number' => $order->get_order_number(), | |
'created_at' => $order->get_date_created()->date('Ymd'), | |
'updated_at' => $order->get_date_modified()->date('Ymd'), | |
'completed_at' => !empty($order->get_date_completed()) ? $order->get_date_completed()->date('Ymd') : '', | |
'status' => $order->get_status(), | |
'currency' => $order->get_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->get_order_key(), | |
'payment_details' => array( | |
'method_id' => $order->get_payment_method(), | |
'method_title' => $order->get_payment_method_title(), | |
'paid_at' => !empty($order->get_date_paid()) ? $order->get_date_paid()->date('Y - m - dH:i:s') : '', | |
), | |
'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(), | |
'formated_state' => WC()->countries->states[$order->get_billing_country()][$order->get_billing_state()], //human readable formated state name | |
'postcode' => $order->get_billing_postcode(), | |
'country' => $order->get_billing_country(), | |
'formated_country' => WC()->countries->countries[$order->get_billing_country()], //human readable formated country name | |
'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(), | |
'formated_state' => WC()->countries->states[$order->get_shipping_country()][$order->get_shipping_state()], //human readable formated state name | |
'postcode' => $order->get_shipping_postcode(), | |
'country' => $order->get_shipping_country(), | |
'formated_country' => WC()->countries->countries[$order->get_shipping_country()], //human readable formated country name | |
), | |
'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(), | |
'customer' => array(), | |
'view_order_url' => $order->get_view_order_url(), | |
'line_items' => array(), | |
'shipping_lines' => array(), | |
'tax_lines' => array(), | |
'fee_lines' => array(), | |
'coupon_lines' => array(), | |
); | |
if ($order->get_user_id() != null) { | |
$customer = new WC_Customer($order->get_user_id()); | |
$last_order = $customer->get_last_order(); | |
$customer_data = array( | |
'id' => $customer->get_id(), | |
'created_at' => $customer->get_date_created()->date('Ymd'), // API gives UTC times. | |
'email' => $customer->get_email(), | |
'first_name' => $customer->get_first_name(), | |
'last_name' => $customer->get_last_name(), | |
'username' => $customer->get_username(), | |
'last_order_id' => is_object($last_order) ? $last_order->get_id() : null, | |
'last_order_date' => is_object($last_order) ? $last_order->get_date_created()->date('Ymd') : null, // API gives UTC times. | |
'orders_count' => $customer->get_order_count(), | |
'total_spent' => wc_format_decimal($customer->get_total_spent(), 2), | |
'avatar_url' => $customer->get_avatar_url(), | |
'billing_address' => array( | |
'first_name' => $customer->get_billing_first_name(), | |
'last_name' => $customer->get_billing_last_name(), | |
'company' => $customer->get_billing_company(), | |
'address_1' => $customer->get_billing_address_1(), | |
'address_2' => $customer->get_billing_address_2(), | |
'city' => $customer->get_billing_city(), | |
'state' => $customer->get_billing_state(), | |
'postcode' => $customer->get_billing_postcode(), | |
'country' => $customer->get_billing_country(), | |
'email' => $customer->get_billing_email(), | |
'phone' => $customer->get_billing_phone(), | |
), | |
'shipping_address' => array( | |
'first_name' => $customer->get_shipping_first_name(), | |
'last_name' => $customer->get_shipping_last_name(), | |
'company' => $customer->get_shipping_company(), | |
'address_1' => $customer->get_shipping_address_1(), | |
'address_2' => $customer->get_shipping_address_2(), | |
'city' => $customer->get_shipping_city(), | |
'state' => $customer->get_shipping_state(), | |
'postcode' => $customer->get_shipping_postcode(), | |
'country' => $customer->get_shipping_country(), | |
), | |
); | |
$order_data['customer'] = $customer_data; | |
} | |
//getting all line items | |
foreach ($order->get_items() as $item_id => $item) { | |
$product = $item->get_product(); | |
$product_id = null; | |
$product_sku = null; | |
// Check if the product exists. | |
if (is_object($product)) { | |
$product_id = $product->get_id(); | |
$product_sku = $product->get_sku(); | |
} | |
$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), | |
'regular_price' => $product->get_regular_price(), | |
'sale_price' => $product->get_sale_price(), | |
'quantity' => wc_stock_amount($item['qty']), | |
'tax_class' => (!empty($item['tax_class'])) ? $item['tax_class'] : null, | |
'name' => $item['name'], | |
'product_id' => (!empty($item->get_variation_id()) && ('product_variation' === $product->post_type)) ? $product->get_parent_id() : $product_id, | |
'variation_id' => (!empty($item->get_variation_id()) && ('product_variation' === $product->post_type)) ? $product_id : 0, | |
'product_url' => get_permalink($product_id), | |
'product_thumbnail_url' => wp_get_attachment_image_src(get_post_thumbnail_id($product_id), 'thumbnail', TRUE)[0], | |
'sku' => $product_sku, | |
'meta' => wc_display_item_meta($item, array('echo ' => false)), | |
); | |
} | |
//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 $order_data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment