<?php require get_template_directory() . '/inc/init.php';flatsome()->init();// --- 功能 1：在後台訂單頁面顯示物流資訊 (保留 V35 的好功能) --- add_action('add_meta_boxes','v85_add_ecpay_info_metabox');function v85_add_ecpay_info_metabox(){add_meta_box('v24_ecpay_info','📦 綠界超商物流資訊 (跨單同步)','v85_display_ecpay_info_callback','shop_order','side','high')}function v85_display_ecpay_info_callback($post){$order=wc_get_order($post->ID);if (! $order) return;$order_id=$order->get_id();// 判斷父子關係 $parent_id=get_post_meta($order_id,'_yith_wcv_parent_order',true) ?:wp_get_post_parent_id($order_id);$target_id=! empty($parent_id) ? $parent_id :$order_id;$stName=get_post_meta($target_id,'_shipping_cvs_store_name',true);$ecpay_info=get_post_meta($target_id,'_ecpay_shipping_info',true);if (! empty($stName) || ! empty($ecpay_info)){echo '<div style="background: #fdfae8; padding: 10px; border: 1px solid #e6db55;">';echo '<strong>門市：</strong>' . esc_html($stName) . '<br>';if (is_array($ecpay_info)){$info=reset($ecpay_info);echo '<strong>單號：</strong><code>' . esc_html($info['PaymentNo'] ?? '') . '</code>'}echo '</div>'}else{echo '尚無物流資訊'}}// --- 功能 2：最輕量化的子訂單同步 (不觸發額外發信邏輯) --- // 我們使用較低的優先權 (999)，確保在所有結帳程序完成後才做同步 add_action('woocommerce_order_status_changed','v85_silent_sync_suborders',999,3);function v85_silent_sync_suborders($order_id,$old_status,$new_status){// 只有當父訂單變成「處理中」或「已完成」時才同步 if (! in_array($new_status,array('processing','completed'))) return;global $wpdb;// 尋找關聯的子訂單 $sub_order_ids=$wpdb->get_col($wpdb->prepare("
        SELECT post_id FROM {$wpdb->postmeta} 
        WHERE meta_key = '_yith_wcv_parent_order' 
        AND meta_value = %d
    ",$order_id));if (! empty($sub_order_ids)){foreach ($sub_order_ids as $sub_id){$sub_order=wc_get_order($sub_id);if ($sub_order && $sub_order->get_status() !==$new_status){// 使用 true 參數代表「這是自動同步」，並避免觸發過多的 hook $sub_order->update_status($new_status,'4/8版補強：隨父訂單自動同步狀態。',true)}}}}// --- 備註 --- // 這裡我們「不寫」任何關於電子郵件的 trigger 或 filter。 // 讓 WooCommerce 與 YITH 依照 4/8 號當時的預設機制自行發信。