/** * WooCommerce 物流整合功能 * 相容 PHP 8.3 + WooCommerce 10.1.2 * 整合功能: * 1. 後台訂單明細:手動輸入物流單號 * 2. 前台我的帳戶訂單列表:顯示物流單號 + 物流狀態 + 複製/查詢功能 * 3. 前台訂單詳情頁:顯示物流狀態 * 4. 電子郵件:顯示物流單號 * 5. 後台訂單列表:狀態顏色優化 * 6. 後台訂單列表:移除物流單號/狀態欄位(避免拖慢速度) */ if ( ! defined( 'ABSPATH' ) ) exit; /* ----------------------------- * 後台功能 (Backend) * ----------------------------- */ /** * 後台訂單明細頁:新增手動輸入物流單號欄位 */ add_action( 'woocommerce_admin_order_data_after_shipping_address', function( $order ) { woocommerce_wp_text_input( array( 'id' => '_shipping_no', 'label' => '物流單號:', 'placeholder' => '請輸入物流單號', 'value' => get_post_meta( $order->get_id(), '_shipping_no', true ), 'wrapper_class' => 'form-field-wide' ) ); }); /** * 儲存手動物流單號 */ add_action( 'woocommerce_process_shop_order_meta', function( $order_id ) { if ( isset( $_POST['_shipping_no'] ) ) { update_post_meta( $order_id, '_shipping_no', sanitize_text_field( $_POST['_shipping_no'] ) ); } }); /** * 後台訂單列表:調整訂單狀態顏色 */ add_action( 'admin_head', function() { echo ''; }); /** * 後台訂單列表:移除物流狀態 + 物流單號欄位 (效能優化) */ add_filter( 'manage_edit-shop_order_columns', function( $columns ) { unset( $columns['ecpay_status'] ); unset( $columns['shipping_no'] ); return $columns; }, 999 ); /* ----------------------------- * 前台功能 (Frontend) * ----------------------------- */ /** * 前台我的帳戶 → 訂單列表:顯示物流單號 + 物流狀態 */ add_filter( 'woocommerce_my_account_my_orders_columns', function( $columns ) { $columns['shipping_info'] = '物流資訊'; return $columns; }); add_action( 'woocommerce_my_account_my_orders_column_shipping_info', function( $order ) { $shipping_no = get_post_meta( $order->get_id(), '_shipping_no', true ); if ( ! $shipping_no ) { echo '尚未提供'; return; } // 假設物流狀態存在 (可手動輸入或外掛同步) $shipping_status = get_post_meta( $order->get_id(), '_ecpay_status', true ); $status_text = $shipping_status ? $shipping_status : '狀態更新中'; // 查詢連結 (可自行增加更多物流商) $query_url = 'https://eservice.7-11.com.tw/e-tracking/search.aspx?no=' . urlencode( $shipping_no ); echo '
物流單號:'. esc_html( $shipping_no ) .'
'; echo '物流狀態:'. esc_html( $status_text ) .'
'; echo '物流單號:'. esc_html( $shipping_no ) .'
'; } }, 20, 4 );