api = new CZL_API(); // 添加定时任务钩子 add_action('czl_update_tracking_cron', array($this, 'update_all_tracking_info')); // 如果定时任务未设置,则设置它 if (!wp_next_scheduled('czl_update_tracking_cron')) { wp_schedule_event(time(), 'hourly', 'czl_update_tracking_cron'); } } /** * 更新所有活跃运单的轨迹信息 */ public function update_all_tracking_info() { global $wpdb; // 获取所有需要更新的运单 $shipments = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}czl_shipments WHERE status NOT IN ('delivered', 'cancelled', 'failed')" ); foreach ($shipments as $shipment) { $this->update_tracking_info($shipment->tracking_number); } } /** * 更新单个运单的轨迹信息 */ public function update_tracking_info($tracking_number) { global $wpdb; try { // 获取运单信息 $shipment = $wpdb->get_row($wpdb->prepare( "SELECT * FROM {$wpdb->prefix}czl_shipments WHERE tracking_number = %s", $tracking_number )); if (!$shipment) { return; } // 获取轨迹信息 $tracking_info = $this->api->get_tracking($tracking_number); if (!empty($tracking_info['trackDetails'])) { foreach ($tracking_info['trackDetails'] as $detail) { // 检查是否已存在该轨迹记录 $exists = $wpdb->get_var($wpdb->prepare( "SELECT id FROM {$wpdb->prefix}czl_tracking_history WHERE shipment_id = %d AND track_date = %s AND track_content = %s", $shipment->id, $detail['track_date'], $detail['track_content'] )); if (!$exists) { // 插入新的轨迹记录 $wpdb->insert( $wpdb->prefix . 'czl_tracking_history', array( 'shipment_id' => $shipment->id, 'tracking_number' => $tracking_number, 'track_date' => $detail['track_date'], 'track_location' => $detail['track_location'], 'track_content' => $detail['track_content'], 'created_at' => current_time('mysql') ), array('%d', '%s', '%s', '%s', '%s', '%s') ); } } // 更新运单状态 $latest = reset($tracking_info['trackDetails']); $new_status = 'in_transit'; if (strpos($latest['track_content'], '已签收') !== false || strpos($latest['track_content'], 'Delivered') !== false) { $new_status = 'delivered'; } // 更新运单状态 $wpdb->update( $wpdb->prefix . 'czl_shipments', array('status' => $new_status), array('id' => $shipment->id), array('%s'), array('%d') ); // 更新WooCommerce订单状态 $order = wc_get_order($shipment->order_id); if ($order) { $order->update_status($new_status, __('Package status updated from tracking info', 'woo-czl-express')); } } } catch (Exception $e) { error_log('CZL Express Error: Failed to update tracking info - ' . $e->getMessage()); } } /** * 在订单详情页显示跟踪信息 */ public static function display_tracking_info($order) { $tracking_number = get_post_meta($order->get_id(), '_czl_tracking_number', true); if (!$tracking_number) { return; } try { $api = new CZL_API(); $tracking_info = $api->get_tracking($tracking_number); if (!empty($tracking_info)) { echo '
' . __('运单号:', 'woo-czl-express') . '' . esc_html($tracking_number) . '
'; if (!empty($tracking_info['trackDetails'])) { echo '' . __('时间', 'woo-czl-express') . ' | '; echo '' . __('地点', 'woo-czl-express') . ' | '; echo '' . __('状态', 'woo-czl-express') . ' | '; echo '
---|---|---|
' . esc_html($detail['track_date']) . ' | '; echo '' . esc_html($detail['track_location']) . ' | '; echo '' . esc_html($detail['track_content']) . ' | '; echo '
' . __('物流跟踪:', 'woo-czl-express') . ''; echo '' . esc_html($tracking_number) . '
'; } } /** * 在管理员订单页面显示轨迹信息 */ public static function display_admin_tracking_info($order) { $tracking_number = $order->get_meta('_czl_tracking_number'); if (empty($tracking_number)) { return; } ?>' . esc_html($tracking_number) . '' ); ?>
get_meta('_czl_child_numbers'); if (!empty($child_numbers)) { echo '' . __('子单号:', 'woo-czl-express') . ' ' . implode(', ', array_map('esc_html', $child_numbers)) . '
'; } // 显示参考号 $reference_number = $order->get_meta('_czl_reference_number'); if (!empty($reference_number)) { echo '' . __('参考号:', 'woo-czl-express') . ' ' . esc_html($reference_number) . '
'; } // 显示偏远信息 $is_remote = $order->get_meta('_czl_is_remote'); if (!empty($is_remote)) { $remote_text = ''; switch ($is_remote) { case 'Y': $remote_text = __('偏远地区', 'woo-czl-express'); break; case 'A': $remote_text = __('FedEx偏远A级', 'woo-czl-express'); break; case 'B': $remote_text = __('FedEx偏远B级', 'woo-czl-express'); break; case 'C': $remote_text = __('FedEx偏远C级', 'woo-czl-express'); break; case 'N': $remote_text = __('非偏远地区', 'woo-czl-express'); break; } if ($remote_text) { echo '' . __('地区类型:', 'woo-czl-express') . ' ' . esc_html($remote_text) . '
'; } } // 显示住宅信息 $is_residential = $order->get_meta('_czl_is_residential'); if ($is_residential === 'Y') { echo '' . __('地址类型:', 'woo-czl-express') . ' ' . __('住宅地址', 'woo-czl-express') . '
'; } // 显示轨迹信息 $tracking_history = $order->get_meta('_czl_tracking_history'); if (!empty($tracking_history['trackDetails'])) { ?>