Merge pull request 'feat(admin): shipping list create, after-sale filters, order note/actions' (#148) from worktree-zibll-native-admin-menus into main

Reviewed-on: CYL/wordpress-theme-aksrt#148
This commit is contained in:
2026-07-15 02:50:59 +08:00
3 changed files with 412 additions and 64 deletions
+2 -1
View File
@@ -99,7 +99,8 @@ Note: zibll「商品管理」`zibpay_product_page` is **paid article sales**, no
| 2b Shipping/after-sale/coupons/income/rebate | done |
| 3 users.php submenus | done |
| 4 SPA strip | done (ops routes redirect to native) |
| 5+ behavior parity | backlog |
| 5+ behavior parity | in progress — shipping list create + order links; after-sale `s`/type filters + list create + merchant approve/reject/confirm + order links; order admin note (JSON-safe) + complete/refund/close quick actions |
| 5 remaining | coupons/cards UX depth; product/forum runtime field parity; pending-to-ship board; express timeline |
## Dual-UI rule
@@ -31,6 +31,7 @@ class AKSRT_Admin_Pay_Menus {
add_action('admin_menu', [$this, 'register_menus']);
add_action('admin_notices', [$this, 'pending_notices']);
add_action('admin_post_aksrt_pay_update_order_status', [$this, 'handle_update_order_status']);
add_action('admin_post_aksrt_pay_update_order_note', [$this, 'handle_update_order_note']);
add_action('admin_post_aksrt_pay_update_withdrawal', [$this, 'handle_update_withdrawal']);
add_action('admin_post_aksrt_pay_disable_charge_card', [$this, 'handle_disable_charge_card']);
add_action('admin_post_aksrt_pay_batch_charge_cards', [$this, 'handle_batch_charge_cards']);
@@ -38,6 +39,7 @@ class AKSRT_Admin_Pay_Menus {
add_action('admin_post_aksrt_pay_create_shipping', [$this, 'handle_create_shipping']);
add_action('admin_post_aksrt_pay_update_after_sale', [$this, 'handle_update_after_sale']);
add_action('admin_post_aksrt_pay_create_after_sale', [$this, 'handle_create_after_sale']);
add_action('admin_post_aksrt_pay_after_sale_action', [$this, 'handle_after_sale_action']);
add_action('admin_post_aksrt_pay_create_coupon', [$this, 'handle_create_coupon']);
add_action('admin_post_aksrt_pay_disable_coupon', [$this, 'handle_disable_coupon']);
add_action('admin_post_aksrt_pay_settle_income', [$this, 'handle_settle_income']);
@@ -196,6 +198,10 @@ class AKSRT_Admin_Pay_Menus {
$map = [
'order_updated' => __('订单状态已更新。', 'aksrt'),
'order_failed' => __('订单状态更新失败。', 'aksrt'),
'order_note_updated' => __('订单备注已保存。', 'aksrt'),
'order_note_failed' => __('订单备注保存失败。', 'aksrt'),
'after_sale_action_ok' => __('售后操作已完成。', 'aksrt'),
'after_sale_action_failed' => __('售后操作失败。', 'aksrt'),
'withdraw_updated' => __('提现状态已更新。', 'aksrt'),
'withdraw_failed' => __('提现状态更新失败。', 'aksrt'),
'card_disabled' => __('充值卡已禁用。', 'aksrt'),
@@ -368,6 +374,7 @@ class AKSRT_Admin_Pay_Menus {
echo '<td>' . esc_html((string) ($row['created_at'] ?? '')) . '</td>';
echo '<td style="display:flex;gap:6px;flex-wrap:wrap;align-items:center;">';
echo '<a class="button button-small" href="' . esc_url($this->page_url('aksrt_orders', ['order' => $order_number])) . '">' . esc_html__('详情', 'aksrt') . '</a>';
$this->render_order_quick_status_buttons($order_number, $status_key);
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:flex;gap:4px;align-items:center;">';
echo '<input type="hidden" name="action" value="aksrt_pay_update_order_status" />';
echo '<input type="hidden" name="order_number" value="' . esc_attr($order_number) . '" />';
@@ -422,6 +429,12 @@ class AKSRT_Admin_Pay_Menus {
$user_id = (int) ($order['user_id'] ?? 0);
$user = $user_id > 0 ? get_userdata($user_id) : null;
$status = (string) ($order['status'] ?? '');
$meta = is_array($order['meta_payload'] ?? null) ? $order['meta_payload'] : [];
$customer_note = (string) ($context['customer_note'] ?? $order['note_text'] ?? '');
$admin_note = is_array($meta) ? (string) ($meta['admin_note'] ?? '') : '';
if ($admin_note === '' && $customer_note === '' && !empty($order['note']) && !is_array(json_decode((string) $order['note'], true))) {
$admin_note = (string) $order['note'];
}
$shipping_items = class_exists('AKSRT_Shipping')
? AKSRT_Shipping::get_instance()->get_by_order($order_number)
@@ -444,7 +457,7 @@ class AKSRT_Admin_Pay_Menus {
__('交易号', 'aksrt') => (string) ($order['payment_transaction_id'] ?? ''),
__('创建时间', 'aksrt') => (string) ($order['created_at'] ?? ''),
__('支付时间', 'aksrt') => (string) ($order['paid_at'] ?? ''),
__('备注', 'aksrt') => (string) ($order['note'] ?? ''),
__('买家备注', 'aksrt') => $customer_note,
];
foreach ($rows as $label => $value) {
echo '<tr><th style="width:160px;">' . esc_html($label) . '</th><td>' . esc_html((string) $value) . '</td></tr>';
@@ -454,7 +467,19 @@ class AKSRT_Admin_Pay_Menus {
}
echo '</tbody></table>';
echo '<h2 style="margin-top:20px;">' . esc_html__('运营备注', 'aksrt') . '</h2>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="max-width:960px;">';
echo '<input type="hidden" name="action" value="aksrt_pay_update_order_note" />';
echo '<input type="hidden" name="order_number" value="' . esc_attr($order_number) . '" />';
wp_nonce_field('aksrt_pay_update_order_note_' . $order_number);
echo '<p><textarea name="admin_note" rows="3" class="large-text">' . esc_textarea($admin_note) . '</textarea></p>';
submit_button(__('保存备注', 'aksrt'), 'secondary', '', false);
echo '</form>';
echo '<h2 style="margin-top:20px;">' . esc_html__('更新状态', 'aksrt') . '</h2>';
echo '<div style="display:flex;gap:8px;flex-wrap:wrap;align-items:center;margin-bottom:10px;">';
$this->render_order_quick_status_buttons($order_number, $status, true);
echo '</div>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:flex;gap:8px;align-items:center;">';
echo '<input type="hidden" name="action" value="aksrt_pay_update_order_status" />';
echo '<input type="hidden" name="order_number" value="' . esc_attr($order_number) . '" />';
@@ -472,24 +497,43 @@ class AKSRT_Admin_Pay_Menus {
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="margin:8px 0 16px;display:flex;flex-wrap:wrap;gap:8px;align-items:end;">';
echo '<input type="hidden" name="action" value="aksrt_pay_create_shipping" />';
echo '<input type="hidden" name="order_number" value="' . esc_attr($order_number) . '" />';
echo '<input type="hidden" name="redirect_page" value="order" />';
wp_nonce_field('aksrt_pay_create_shipping_' . $order_number);
echo '<p><label>' . esc_html__('承运商', 'aksrt') . '<br><input type="text" name="carrier" required placeholder="SF/YTO/..." /></label></p>';
echo '<p><label>' . esc_html__('承运商', 'aksrt') . '<br>';
$this->render_carrier_field('');
echo '</label></p>';
echo '<p><label>' . esc_html__('运单号', 'aksrt') . '<br><input type="text" name="tracking_no" required /></label></p>';
echo '<p><label>' . esc_html__('备注', 'aksrt') . '<br><input type="text" name="note" /></label></p>';
submit_button(__('创建发货', 'aksrt'), 'secondary', '', false);
echo ' <a class="button" href="' . esc_url($this->page_url('aksrt_shipping')) . '">' . esc_html__('物流管理', 'aksrt') . '</a>';
echo ' <a class="button" href="' . esc_url($this->page_url('aksrt_shipping', ['s' => $order_number])) . '">' . esc_html__('物流管理', 'aksrt') . '</a>';
echo '</form>';
if (!$shipping_items) {
echo '<p>' . esc_html__('暂无物流记录。', 'aksrt') . '</p>';
} else {
echo '<table class="widefat striped"><thead><tr><th>ID</th><th>' . esc_html__('承运商', 'aksrt') . '</th><th>' . esc_html__('运单号', 'aksrt') . '</th><th>' . esc_html__('状态', 'aksrt') . '</th><th>' . esc_html__('时间', 'aksrt') . '</th></tr></thead><tbody>';
$status_options = $this->shipping_status_options(false);
echo '<table class="widefat striped"><thead><tr><th>ID</th><th>' . esc_html__('承运商', 'aksrt') . '</th><th>' . esc_html__('运单号', 'aksrt') . '</th><th>' . esc_html__('状态', 'aksrt') . '</th><th>' . esc_html__('时间', 'aksrt') . '</th><th>' . esc_html__('操作', 'aksrt') . '</th></tr></thead><tbody>';
foreach ($shipping_items as $item) {
$sid = (int) ($item['id'] ?? 0);
$st = (string) ($item['status'] ?? '');
echo '<tr>';
echo '<td>' . esc_html((string) ($item['id'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) $sid) . '</td>';
echo '<td>' . esc_html((string) ($item['carrier'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) ($item['tracking_no'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) ($item['status'] ?? '')) . '</td>';
echo '<td>' . esc_html($status_options[$st] ?? $st) . '</td>';
echo '<td>' . esc_html((string) ($item['created_at'] ?? '')) . '</td>';
echo '<td><form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:flex;gap:4px;align-items:center;">';
echo '<input type="hidden" name="action" value="aksrt_pay_update_shipping" />';
echo '<input type="hidden" name="shipping_id" value="' . esc_attr((string) $sid) . '" />';
echo '<input type="hidden" name="redirect_order" value="' . esc_attr($order_number) . '" />';
wp_nonce_field('aksrt_pay_update_shipping_' . $sid);
echo '<select name="new_status">';
foreach ($status_options as $key => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($st, $key, false), esc_html($label));
}
echo '</select>';
echo '<input type="text" name="note" placeholder="' . esc_attr__('备注', 'aksrt') . '" />';
submit_button(__('更新', 'aksrt'), 'small', '', false);
echo '</form></td>';
echo '</tr>';
}
echo '</tbody></table>';
@@ -500,14 +544,10 @@ class AKSRT_Admin_Pay_Menus {
echo '<input type="hidden" name="action" value="aksrt_pay_create_after_sale" />';
echo '<input type="hidden" name="order_number" value="' . esc_attr($order_number) . '" />';
echo '<input type="hidden" name="user_id" value="' . esc_attr((string) $user_id) . '" />';
echo '<input type="hidden" name="redirect_page" value="order" />';
wp_nonce_field('aksrt_pay_create_after_sale_' . $order_number);
echo '<p><label>' . esc_html__('类型', 'aksrt') . '<br><select name="type">';
foreach ([
'refund' => __('仅退款', 'aksrt'),
'refund_return' => __('退货退款', 'aksrt'),
'exchange' => __('换货', 'aksrt'),
'repair' => __('维修', 'aksrt'),
] as $k => $label) {
foreach ($this->after_sale_type_options(false) as $k => $label) {
printf('<option value="%s">%s</option>', esc_attr($k), esc_html($label));
}
echo '</select></label></p>';
@@ -520,13 +560,15 @@ class AKSRT_Admin_Pay_Menus {
if (!$after_sale) {
echo '<p>' . esc_html__('暂无售后工单。', 'aksrt') . '</p>';
} else {
echo '<table class="widefat striped"><thead><tr><th>ID</th><th>' . esc_html__('类型', 'aksrt') . '</th><th>' . esc_html__('状态', 'aksrt') . '</th><th>' . esc_html__('时间', 'aksrt') . '</th></tr></thead><tbody>';
echo '<table class="widefat striped"><thead><tr><th>ID</th><th>' . esc_html__('类型', 'aksrt') . '</th><th>' . esc_html__('状态', 'aksrt') . '</th><th>' . esc_html__('时间', 'aksrt') . '</th><th>' . esc_html__('操作', 'aksrt') . '</th></tr></thead><tbody>';
foreach ($after_sale as $item) {
$tid = (int) ($item['id'] ?? 0);
echo '<tr>';
echo '<td>' . esc_html((string) ($item['id'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) ($item['type'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) ($item['status'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) $tid) . '</td>';
echo '<td>' . esc_html((string) ($item['type_label'] ?? $item['type'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) ($item['status_label'] ?? $item['status'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) ($item['created_at'] ?? '')) . '</td>';
echo '<td><a href="' . esc_url($this->page_url('aksrt_after_sale', ['s' => $order_number])) . '">' . esc_html__('在售后页处理', 'aksrt') . '</a></td>';
echo '</tr>';
}
echo '</tbody></table>';
@@ -557,9 +599,26 @@ class AKSRT_Admin_Pay_Menus {
exit;
}
/* ------------------------------------------------------------------
* Withdrawals
* ----------------------------------------------------------------*/
public function handle_update_order_note(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$order_number = isset($_POST['order_number']) ? sanitize_text_field((string) wp_unslash($_POST['order_number'])) : '';
$admin_note = isset($_POST['admin_note']) ? sanitize_textarea_field((string) wp_unslash($_POST['admin_note'])) : '';
check_admin_referer('aksrt_pay_update_order_note_' . $order_number);
$ok = false;
if ($order_number !== '' && class_exists('AKSRT_Order_Repository')) {
$ok = AKSRT_Order_Repository::get_instance()->update_admin_note($order_number, $admin_note);
}
wp_safe_redirect($this->page_url('aksrt_orders', [
'order' => $order_number,
'aksrt_pay_msg' => $ok ? 'order_note_updated' : 'order_note_failed',
]));
exit;
}
public function render_withdrawals_page(): void {
$this->ensure_cap();
@@ -960,6 +1019,7 @@ class AKSRT_Admin_Pay_Menus {
$per_page = 20;
$svc = AKSRT_Shipping::get_instance();
$stats = $svc->get_stats();
$status_options = $this->shipping_status_options(true);
echo '<p>' . esc_html(sprintf(
/* translators: 1: total */
@@ -967,16 +1027,23 @@ class AKSRT_Admin_Pay_Menus {
(int) ($stats['total'] ?? 0)
)) . '</p>';
echo '<h2>' . esc_html__('新建发货', 'aksrt') . '</h2>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="margin:8px 0 20px;display:flex;flex-wrap:wrap;gap:8px;align-items:end;">';
echo '<input type="hidden" name="action" value="aksrt_pay_create_shipping" />';
echo '<input type="hidden" name="redirect_page" value="shipping" />';
wp_nonce_field('aksrt_pay_create_shipping');
echo '<p><label>' . esc_html__('订单号', 'aksrt') . '<br><input type="text" name="order_number" value="' . esc_attr($search) . '" required class="regular-text" /></label></p>';
echo '<p><label>' . esc_html__('承运商', 'aksrt') . '<br>';
$this->render_carrier_field('');
echo '</label></p>';
echo '<p><label>' . esc_html__('运单号', 'aksrt') . '<br><input type="text" name="tracking_no" required /></label></p>';
echo '<p><label>' . esc_html__('备注', 'aksrt') . '<br><input type="text" name="note" /></label></p>';
submit_button(__('创建发货', 'aksrt'), 'primary', '', false);
echo '</form>';
echo '<form method="get" style="margin:12px 0;">';
echo '<input type="hidden" name="page" value="aksrt_shipping" />';
echo '<select name="status">';
$status_options = [
'' => __('全部状态', 'aksrt'),
'shipped' => __('已发货', 'aksrt'),
'in_transit' => __('运输中', 'aksrt'),
'delivered' => __('已签收', 'aksrt'),
'returned' => __('已退回', 'aksrt'),
];
foreach ($status_options as $key => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($status, $key, false), esc_html($label));
}
@@ -1001,6 +1068,7 @@ class AKSRT_Admin_Pay_Menus {
$items = $result['items'] ?? [];
$total = (int) ($result['total'] ?? 0);
$pages = max(1, (int) ceil(max(1, $total) / $per_page));
$row_status_options = $this->shipping_status_options(false);
echo '<table class="wp-list-table widefat fixed striped"><thead><tr>';
echo '<th>ID</th><th>' . esc_html__('订单号', 'aksrt') . '</th><th>' . esc_html__('承运商', 'aksrt') . '</th><th>' . esc_html__('运单号', 'aksrt') . '</th><th>' . esc_html__('状态', 'aksrt') . '</th><th>' . esc_html__('时间', 'aksrt') . '</th><th>' . esc_html__('操作', 'aksrt') . '</th>';
@@ -1011,22 +1079,20 @@ class AKSRT_Admin_Pay_Menus {
foreach ($items as $row) {
$id = (int) ($row['id'] ?? 0);
$st = (string) ($row['status'] ?? '');
$order_number = (string) ($row['order_number'] ?? '');
echo '<tr>';
echo '<td>' . esc_html((string) $id) . '</td>';
echo '<td><code>' . esc_html((string) ($row['order_number'] ?? '')) . '</code></td>';
echo '<td><a href="' . esc_url($this->page_url('aksrt_orders', ['order' => $order_number])) . '"><code>' . esc_html($order_number) . '</code></a></td>';
echo '<td>' . esc_html((string) ($row['carrier'] ?? '')) . '</td>';
echo '<td>' . esc_html((string) ($row['tracking_no'] ?? '')) . '</td>';
echo '<td>' . esc_html($status_options[$st] ?? $st) . '</td>';
echo '<td>' . esc_html($row_status_options[$st] ?? $st) . '</td>';
echo '<td>' . esc_html((string) ($row['created_at'] ?? '')) . '</td>';
echo '<td><form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:flex;gap:4px;align-items:center;">';
echo '<input type="hidden" name="action" value="aksrt_pay_update_shipping" />';
echo '<input type="hidden" name="shipping_id" value="' . esc_attr((string) $id) . '" />';
wp_nonce_field('aksrt_pay_update_shipping_' . $id);
echo '<select name="new_status">';
foreach ($status_options as $key => $label) {
if ($key === '') {
continue;
}
foreach ($row_status_options as $key => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($st, $key, false), esc_html($label));
}
echo '</select>';
@@ -1052,6 +1118,16 @@ class AKSRT_Admin_Pay_Menus {
if ($id > 0 && $new_status !== '' && class_exists('AKSRT_Shipping')) {
$ok = AKSRT_Shipping::get_instance()->update_status($id, $new_status, $note, get_current_user_id());
}
$redirect_order = isset($_POST['redirect_order']) ? sanitize_text_field((string) wp_unslash($_POST['redirect_order'])) : '';
if ($redirect_order !== '') {
wp_safe_redirect($this->page_url('aksrt_orders', [
'order' => $redirect_order,
'aksrt_pay_msg' => $ok ? 'shipping_updated' : 'shipping_failed',
]));
exit;
}
wp_safe_redirect($this->page_url('aksrt_shipping', ['aksrt_pay_msg' => $ok ? 'shipping_updated' : 'shipping_failed']));
exit;
}
@@ -1061,10 +1137,18 @@ class AKSRT_Admin_Pay_Menus {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$order_number = isset($_POST['order_number']) ? sanitize_text_field((string) wp_unslash($_POST['order_number'])) : '';
check_admin_referer('aksrt_pay_create_shipping_' . $order_number);
$redirect_page = isset($_POST['redirect_page']) ? sanitize_key((string) wp_unslash($_POST['redirect_page'])) : 'order';
$nonce_action = $redirect_page === 'shipping' ? 'aksrt_pay_create_shipping' : ('aksrt_pay_create_shipping_' . $order_number);
check_admin_referer($nonce_action);
$ok = false;
if ($order_number !== '' && class_exists('AKSRT_Shipping')) {
if (class_exists('AKSRT_Order_Repository')) {
$order = AKSRT_Order_Repository::get_instance()->get($order_number);
if (!$order) {
$this->redirect_shipping_create_result(false, $order_number, $redirect_page);
}
}
$id = AKSRT_Shipping::get_instance()->create([
'order_number' => $order_number,
'carrier' => sanitize_text_field((string) wp_unslash($_POST['carrier'] ?? '')),
@@ -1075,9 +1159,18 @@ class AKSRT_Admin_Pay_Menus {
$ok = $id > 0;
}
$this->redirect_shipping_create_result($ok, $order_number, $redirect_page);
}
private function redirect_shipping_create_result(bool $ok, string $order_number, string $redirect_page): void {
$msg = $ok ? 'shipping_created' : 'shipping_create_failed';
if ($redirect_page === 'shipping') {
wp_safe_redirect($this->page_url('aksrt_shipping', ['aksrt_pay_msg' => $msg, 's' => $order_number]));
exit;
}
wp_safe_redirect($this->page_url('aksrt_orders', [
'order' => $order_number,
'aksrt_pay_msg' => $ok ? 'shipping_created' : 'shipping_create_failed',
'aksrt_pay_msg' => $msg,
]));
exit;
}
@@ -1087,7 +1180,9 @@ class AKSRT_Admin_Pay_Menus {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$order_number = isset($_POST['order_number']) ? sanitize_text_field((string) wp_unslash($_POST['order_number'])) : '';
check_admin_referer('aksrt_pay_create_after_sale_' . $order_number);
$redirect_page = isset($_POST['redirect_page']) ? sanitize_key((string) wp_unslash($_POST['redirect_page'])) : 'order';
$nonce_action = $redirect_page === 'after_sale' ? 'aksrt_pay_create_after_sale' : ('aksrt_pay_create_after_sale_' . $order_number);
check_admin_referer($nonce_action);
$ok = false;
if ($order_number !== '' && class_exists('AKSRT_After_Sale')) {
@@ -1107,17 +1202,18 @@ class AKSRT_Admin_Pay_Menus {
$ok = $id > 0;
}
$msg = $ok ? 'after_sale_created' : 'after_sale_create_failed';
if ($redirect_page === 'after_sale') {
wp_safe_redirect($this->page_url('aksrt_after_sale', ['aksrt_pay_msg' => $msg, 's' => $order_number]));
exit;
}
wp_safe_redirect($this->page_url('aksrt_orders', [
'order' => $order_number,
'aksrt_pay_msg' => $ok ? 'after_sale_created' : 'after_sale_create_failed',
'aksrt_pay_msg' => $msg,
]));
exit;
}
/* ------------------------------------------------------------------
* After-sale
* ----------------------------------------------------------------*/
public function render_after_sale_page(): void {
$this->ensure_cap();
$this->render_header(__('售后管理', 'aksrt'));
@@ -1129,10 +1225,17 @@ class AKSRT_Admin_Pay_Menus {
}
$status = isset($_GET['status']) ? sanitize_key((string) wp_unslash($_GET['status'])) : '';
$type = isset($_GET['type']) ? sanitize_key((string) wp_unslash($_GET['type'])) : '';
$search = isset($_GET['s']) ? sanitize_text_field((string) wp_unslash($_GET['s'])) : '';
if ($search === '' && isset($_GET['order_number'])) {
$search = sanitize_text_field((string) wp_unslash($_GET['order_number']));
}
$page_num = max(1, (int) ($_GET['paged'] ?? 1));
$per_page = 20;
$svc = AKSRT_After_Sale::get_instance();
$stats = $svc->get_stats();
$status_options = $this->after_sale_status_options(true);
$type_options = $this->after_sale_type_options(true);
echo '<p>' . esc_html(sprintf(
/* translators: 1: total 2: pending */
@@ -1141,15 +1244,22 @@ class AKSRT_Admin_Pay_Menus {
(int) ($stats['pending'] ?? 0)
)) . '</p>';
$status_options = [
'' => __('全部状态', 'aksrt'),
'pending' => __('待处理', 'aksrt'),
'processing' => __('处理中', 'aksrt'),
'approved' => __('已通过', 'aksrt'),
'rejected' => __('已拒绝', 'aksrt'),
'completed' => __('已完成', 'aksrt'),
'cancelled' => __('已取消', 'aksrt'),
];
echo '<h2>' . esc_html__('新建售后', 'aksrt') . '</h2>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="margin:8px 0 20px;display:flex;flex-wrap:wrap;gap:8px;align-items:end;">';
echo '<input type="hidden" name="action" value="aksrt_pay_create_after_sale" />';
echo '<input type="hidden" name="redirect_page" value="after_sale" />';
wp_nonce_field('aksrt_pay_create_after_sale');
echo '<p><label>' . esc_html__('订单号', 'aksrt') . '<br><input type="text" name="order_number" value="' . esc_attr($search) . '" required class="regular-text" /></label></p>';
echo '<p><label>' . esc_html__('类型', 'aksrt') . '<br><select name="type">';
foreach ($this->after_sale_type_options(false) as $k => $label) {
printf('<option value="%s">%s</option>', esc_attr($k), esc_html($label));
}
echo '</select></label></p>';
echo '<p><label>' . esc_html__('金额', 'aksrt') . '<br><input type="number" step="0.01" min="0" name="amount" value="0" /></label></p>';
echo '<p><label>' . esc_html__('原因', 'aksrt') . '<br><input type="text" name="reason" required class="regular-text" /></label></p>';
echo '<p><label>' . esc_html__('备注', 'aksrt') . '<br><input type="text" name="note" /></label></p>';
submit_button(__('创建售后', 'aksrt'), 'primary', '', false);
echo '</form>';
echo '<form method="get" style="margin:12px 0;">';
echo '<input type="hidden" name="page" value="aksrt_after_sale" />';
@@ -1158,17 +1268,29 @@ class AKSRT_Admin_Pay_Menus {
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($status, $key, false), esc_html($label));
}
echo '</select> ';
echo '<select name="type">';
foreach ($type_options as $key => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($type, $key, false), esc_html($label));
}
echo '</select> ';
echo '<input type="search" name="s" value="' . esc_attr($search) . '" placeholder="' . esc_attr__('订单号', 'aksrt') . '" /> ';
submit_button(__('筛选', 'aksrt'), 'secondary', '', false);
echo '</form>';
$result = $svc->get_list([
$list_args = [
'status' => $status,
'type' => $type,
'limit' => $per_page,
'offset' => ($page_num - 1) * $per_page,
]);
];
if ($search !== '') {
$list_args['order_number'] = $search;
}
$result = $svc->get_list($list_args);
$items = $result['items'] ?? [];
$total = (int) ($result['total'] ?? 0);
$pages = max(1, (int) ceil(max(1, $total) / $per_page));
$row_status_options = $this->after_sale_status_options(false);
echo '<table class="wp-list-table widefat fixed striped"><thead><tr>';
echo '<th>ID</th><th>' . esc_html__('订单号', 'aksrt') . '</th><th>' . esc_html__('类型', 'aksrt') . '</th><th>' . esc_html__('用户', 'aksrt') . '</th><th>' . esc_html__('状态', 'aksrt') . '</th><th>' . esc_html__('时间', 'aksrt') . '</th><th>' . esc_html__('操作', 'aksrt') . '</th>';
@@ -1181,28 +1303,53 @@ class AKSRT_Admin_Pay_Menus {
$user_id = (int) ($row['user_id'] ?? 0);
$user = $user_id > 0 ? get_userdata($user_id) : null;
$st = (string) ($row['status'] ?? '');
$order_number = (string) ($row['order_number'] ?? '');
echo '<tr>';
echo '<td>' . esc_html((string) $id) . '</td>';
echo '<td><code>' . esc_html((string) ($row['order_number'] ?? '')) . '</code></td>';
echo '<td>' . esc_html((string) ($row['type'] ?? '')) . '</td>';
echo '<td><a href="' . esc_url($this->page_url('aksrt_orders', ['order' => $order_number])) . '"><code>' . esc_html($order_number) . '</code></a></td>';
echo '<td>' . esc_html((string) ($row['type_label'] ?? $type_options[$row['type'] ?? ''] ?? $row['type'] ?? '')) . '</td>';
echo '<td>' . ($user ? esc_html($user->display_name) : esc_html((string) $user_id)) . '</td>';
echo '<td>' . esc_html($status_options[$st] ?? $st) . '</td>';
echo '<td>' . esc_html((string) ($row['status_label'] ?? $row_status_options[$st] ?? $st)) . '</td>';
echo '<td>' . esc_html((string) ($row['created_at'] ?? '')) . '</td>';
echo '<td><form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:flex;gap:4px;align-items:center;flex-wrap:wrap;">';
echo '<td style="display:flex;gap:6px;flex-wrap:wrap;align-items:center;">';
if ($st === 'pending') {
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
echo '<input type="hidden" name="action" value="aksrt_pay_after_sale_action" />';
echo '<input type="hidden" name="ticket_id" value="' . esc_attr((string) $id) . '" />';
echo '<input type="hidden" name="as_action" value="approve" />';
wp_nonce_field('aksrt_pay_after_sale_action_' . $id);
submit_button(__('同意', 'aksrt'), 'small', '', false);
echo '</form>';
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
echo '<input type="hidden" name="action" value="aksrt_pay_after_sale_action" />';
echo '<input type="hidden" name="ticket_id" value="' . esc_attr((string) $id) . '" />';
echo '<input type="hidden" name="as_action" value="reject" />';
wp_nonce_field('aksrt_pay_after_sale_action_' . $id);
echo '<button type="submit" class="button button-small" onclick="return confirm(\'' . esc_js(__('确认拒绝该售后?', 'aksrt')) . '\');">' . esc_html__('拒绝', 'aksrt') . '</button>';
echo '</form>';
}
if ($st === 'processing') {
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '">';
echo '<input type="hidden" name="action" value="aksrt_pay_after_sale_action" />';
echo '<input type="hidden" name="ticket_id" value="' . esc_attr((string) $id) . '" />';
echo '<input type="hidden" name="as_action" value="confirm_return" />';
wp_nonce_field('aksrt_pay_after_sale_action_' . $id);
submit_button(__('确认收货', 'aksrt'), 'small', '', false);
echo '</form>';
}
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:flex;gap:4px;align-items:center;flex-wrap:wrap;">';
echo '<input type="hidden" name="action" value="aksrt_pay_update_after_sale" />';
echo '<input type="hidden" name="ticket_id" value="' . esc_attr((string) $id) . '" />';
wp_nonce_field('aksrt_pay_update_after_sale_' . $id);
echo '<select name="new_status">';
foreach ($status_options as $key => $label) {
if ($key === '') {
continue;
}
foreach ($row_status_options as $key => $label) {
printf('<option value="%s"%s>%s</option>', esc_attr($key), selected($st, $key, false), esc_html($label));
}
echo '</select>';
echo '<input type="text" name="note" placeholder="' . esc_attr__('备注', 'aksrt') . '" />';
submit_button(__('更新', 'aksrt'), 'small', '', false);
echo '</form></td></tr>';
echo '</form>';
echo '</td></tr>';
}
echo '</tbody></table>';
$this->render_pagination($pages, $page_num);
@@ -1226,9 +1373,32 @@ class AKSRT_Admin_Pay_Menus {
exit;
}
/* ------------------------------------------------------------------
* Coupons
* ----------------------------------------------------------------*/
public function handle_after_sale_action(): void {
if (!current_user_can(self::CAPABILITY)) {
wp_die(esc_html__('权限不足', 'aksrt'));
}
$id = isset($_POST['ticket_id']) ? (int) $_POST['ticket_id'] : 0;
$as_action = isset($_POST['as_action']) ? sanitize_key((string) wp_unslash($_POST['as_action'])) : '';
check_admin_referer('aksrt_pay_after_sale_action_' . $id);
$ok = false;
if ($id > 0 && class_exists('AKSRT_After_Sale')) {
$svc = AKSRT_After_Sale::get_instance();
$operator_id = get_current_user_id();
if ($as_action === 'approve') {
$ok = $svc->merchant_approve($id, $operator_id);
} elseif ($as_action === 'reject') {
$ok = $svc->merchant_reject($id, $operator_id, __('商家拒绝', 'aksrt'));
} elseif ($as_action === 'confirm_return') {
$ok = $svc->merchant_confirm_return($id, $operator_id);
}
}
wp_safe_redirect($this->page_url('aksrt_after_sale', [
'aksrt_pay_msg' => $ok ? 'after_sale_action_ok' : 'after_sale_action_failed',
]));
exit;
}
public function render_coupons_page(): void {
$this->ensure_cap();
@@ -1553,6 +1723,143 @@ class AKSRT_Admin_Pay_Menus {
exit;
}
/**
* @return array<string, string>
*/
private function shipping_status_options(bool $include_all = false): array {
$options = [];
if ($include_all) {
$options[''] = __('全部状态', 'aksrt');
}
if (class_exists('AKSRT_Shipping')) {
foreach (AKSRT_Shipping::get_status_options() as $item) {
$value = (string) ($item['value'] ?? '');
if ($value === '') {
continue;
}
$options[$value] = (string) ($item['label'] ?? $value);
}
}
if (count($options) === (int) $include_all) {
$options = array_merge($options, [
'shipped' => __('已发货', 'aksrt'),
'in_transit' => __('运输中', 'aksrt'),
'delivered' => __('已签收', 'aksrt'),
'returned' => __('已退回', 'aksrt'),
]);
}
return $options;
}
/**
* @return array<string, string>
*/
private function after_sale_status_options(bool $include_all = false): array {
$options = [];
if ($include_all) {
$options[''] = __('全部状态', 'aksrt');
}
if (class_exists('AKSRT_After_Sale')) {
foreach (AKSRT_After_Sale::get_status_options() as $item) {
$value = (string) ($item['value'] ?? '');
if ($value === '') {
continue;
}
$options[$value] = (string) ($item['label'] ?? $value);
}
}
return $options;
}
/**
* @return array<string, string>
*/
private function after_sale_type_options(bool $include_all = false): array {
$options = [];
if ($include_all) {
$options[''] = __('全部类型', 'aksrt');
}
// Keep operational create options broader than public get_type_options().
$defaults = [
'refund' => __('仅退款', 'aksrt'),
'refund_return' => __('退货退款', 'aksrt'),
'exchange' => __('换货', 'aksrt'),
'repair' => __('维修', 'aksrt'),
];
if (class_exists('AKSRT_After_Sale')) {
foreach (AKSRT_After_Sale::get_type_options() as $item) {
$value = (string) ($item['value'] ?? '');
if ($value === '') {
continue;
}
$defaults[$value] = (string) ($item['label'] ?? $value);
}
}
return array_merge($options, $defaults);
}
private function render_carrier_field(string $selected = ''): void {
$options = [];
if (class_exists('AKSRT_Shipping')) {
foreach (AKSRT_Shipping::get_carrier_options() as $item) {
$value = (string) ($item['value'] ?? '');
$label = (string) ($item['label'] ?? $value);
if ($value !== '') {
$options[$value] = $label;
}
}
}
if ($options) {
echo '<select name="carrier" required>';
foreach ($options as $value => $label) {
printf(
'<option value="%s"%s>%s</option>',
esc_attr($value),
selected($selected, $value, false),
esc_html($label)
);
}
echo '</select>';
return;
}
echo '<input type="text" name="carrier" value="' . esc_attr($selected) . '" required placeholder="SF/YTO/..." />';
}
private function render_order_quick_status_buttons(string $order_number, string $current_status, bool $redirect_detail = false): void {
$actions = [
'completed' => __('完成', 'aksrt'),
'refunded' => __('退款', 'aksrt'),
'cancelled' => __('关闭', 'aksrt'),
];
foreach ($actions as $status => $label) {
if ($status === $current_status) {
continue;
}
$confirm = '';
if ($status === 'refunded') {
$confirm = __('确认将该订单标记为已退款?', 'aksrt');
} elseif ($status === 'cancelled') {
$confirm = __('确认关闭该订单?', 'aksrt');
} elseif ($status === 'completed') {
$confirm = __('确认完成该订单?', 'aksrt');
}
echo '<form method="post" action="' . esc_url(admin_url('admin-post.php')) . '" style="display:inline;">';
echo '<input type="hidden" name="action" value="aksrt_pay_update_order_status" />';
echo '<input type="hidden" name="order_number" value="' . esc_attr($order_number) . '" />';
echo '<input type="hidden" name="new_status" value="' . esc_attr($status) . '" />';
if ($redirect_detail) {
echo '<input type="hidden" name="redirect_order" value="' . esc_attr($order_number) . '" />';
}
wp_nonce_field('aksrt_pay_update_order_status_' . $order_number);
printf(
'<button type="submit" class="button button-small"%s>%s</button>',
$confirm !== '' ? ' onclick="return confirm(\'' . esc_js($confirm) . '\');"' : '',
esc_html($label)
);
echo '</form>';
}
}
private function render_pagination(int $pages, int $page_num): void {
if ($pages <= 1) {
return;
@@ -232,6 +232,46 @@ class AKSRT_Order_Repository {
/**
* 更新订单状态(带状态机验证)
*/
/**
* Merge operator admin note into order note meta without destroying JSON payload.
*/
public function update_admin_note(string $order_number, string $admin_note): bool {
global $wpdb;
$order = $this->get($order_number);
if (!$order) {
return false;
}
$raw = isset($order['note']) && is_string($order['note']) ? $order['note'] : '';
$decoded = $raw !== '' ? json_decode($raw, true) : null;
$admin_note = sanitize_textarea_field($admin_note);
if (is_array($decoded)) {
$decoded['admin_note'] = $admin_note;
$new_note = wp_json_encode($decoded, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
} elseif ($raw !== '') {
$new_note = wp_json_encode([
'customer_note' => $raw,
'admin_note' => $admin_note,
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
} else {
$new_note = wp_json_encode([
'admin_note' => $admin_note,
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
$result = $wpdb->update(
$this->table(),
['note' => $new_note],
['order_number' => $order_number],
['%s'],
['%s']
);
return $result !== false;
}
public function update_status(string $order_number, string $new_status, array $extra = []): bool {
global $wpdb;