feat(shop): complete storefront archive, seller edit, revenue

Wire product cat/tag archives to settings-driven sort and AJAX paging,
seller product options/video/params editing, revenue withdraw form and
order review submit — finishing remaining shop frontend gaps after buy-path.
This commit is contained in:
2026-07-12 10:38:02 +08:00
parent 5a8f08db8a
commit f3db6c89cc
17 changed files with 1024 additions and 248 deletions
@@ -354,6 +354,190 @@ trait AKSRT_Admin_Shop_Controller_Frontend_Flow_Operations {
return $this->success(['message' => __('已设为默认地址', 'aksrt')]);
}
public function get_frontend_product_archive(WP_REST_Request $request): WP_REST_Response {
if (!class_exists('AKSRT_Shop')) {
return $this->error('商城服务不可用', 500);
}
$taxonomy = sanitize_key((string) ($request->get_param('taxonomy') ?? ''));
$term_id = (int) ($request->get_param('term_id') ?? 0);
$page = max(1, (int) ($request->get_param('page') ?? 1));
$orderby = sanitize_key((string) ($request->get_param('orderby') ?? ''));
$view = AKSRT_Shop::get_instance()->get_product_archive_view([
'taxonomy' => $taxonomy,
'term_id' => $term_id,
'paged' => $page,
'orderby' => $orderby,
]);
$html = '';
if (!empty($view['products']) && is_array($view['products'])) {
ob_start();
foreach ($view['products'] as $product) {
$this->render_product_card_html($product, $view);
}
$html = (string) ob_get_clean();
}
return $this->success([
'data' => array_merge($view, [
'html' => $html,
'has_more' => (int) ($view['page'] ?? 1) < (int) ($view['max_pages'] ?? 1),
]),
]);
}
private function render_product_card_html(array $product, array $view = []): void {
$sales_cfg = is_array($view['sales'] ?? null) ? $view['sales'] : [];
$mode = (string) ($sales_cfg['show'] ?? 'show');
$min_sales = max(0, (int) ($sales_cfg['show_min'] ?? 0));
$sales = (int) ($product['sales'] ?? 0);
$can_show_sales = false;
if ($mode === 'show') {
$can_show_sales = $sales > 0;
} elseif ($mode === 'threshold') {
$can_show_sales = $sales >= $min_sales;
}
$permalink = (string) ($product['permalink'] ?? '#');
$title = (string) ($product['title'] ?? '');
$image = (string) ($product['image'] ?? '');
$price = (float) ($product['price'] ?? 0);
$excerpt = (string) ($product['excerpt'] ?? '');
?>
<article class="aksrt-product-card">
<a href="<?php echo esc_url($permalink); ?>" class="aksrt-product-card__link">
<?php if ($image !== '') : ?>
<div class="aksrt-product-card__image">
<img src="<?php echo esc_url($image); ?>" alt="<?php echo esc_attr($title); ?>" loading="lazy">
</div>
<?php else : ?>
<div class="aksrt-product-card__image aksrt-product-card__image--placeholder">
<i class="fas fa-image"></i>
</div>
<?php endif; ?>
<div class="aksrt-product-card__body">
<h3 class="aksrt-product-card__title"><?php echo esc_html($title); ?></h3>
<?php if ($excerpt !== '') : ?>
<p class="aksrt-product-card__excerpt"><?php echo esc_html(wp_trim_words($excerpt, 15)); ?></p>
<?php endif; ?>
<div class="aksrt-product-card__footer">
<span class="aksrt-product-card__price">¥<?php echo esc_html(number_format($price, 2)); ?></span>
<?php if ($can_show_sales) : ?>
<span class="aksrt-product-card__sales"><?php printf(esc_html__('已售 %d', 'aksrt'), $sales); ?></span>
<?php endif; ?>
</div>
</div>
</a>
</article>
<?php
}
public function get_frontend_revenue(WP_REST_Request $request): WP_REST_Response {
if (!function_exists('aksrt_get_revenue_page_view')) {
return $this->error('收益服务不可用', 500);
}
$view = aksrt_get_revenue_page_view(get_current_user_id());
$withdrawals = [];
if (class_exists('AKSRT_Withdrawal')) {
$rows = AKSRT_Withdrawal::get_instance()->get_user_withdrawals(get_current_user_id(), ['limit' => 20]);
foreach ($rows as $row) {
$withdrawals[] = [
'id' => (int) ($row['id'] ?? 0),
'amount' => (float) ($row['amount'] ?? 0),
'status' => (string) ($row['status'] ?? ''),
'method' => (string) ($row['method'] ?? ''),
'created_at' => (string) ($row['created_at'] ?? ''),
'note' => (string) ($row['note'] ?? ''),
];
}
}
$settings = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_withdrawal_settings() : [];
return $this->success([
'data' => array_merge($view, [
'withdrawals' => $withdrawals,
'settings' => [
'min_amount' => (float) ($settings['min_amount'] ?? 50),
'service_charge_rate' => (float) ($settings['service_charge_rate'] ?? 0),
'text' => (string) ($settings['text'] ?? ''),
],
]),
]);
}
public function create_frontend_withdraw(WP_REST_Request $request): WP_REST_Response {
if (!class_exists('AKSRT_Withdrawal') || !class_exists('AKSRT_Credits')) {
return $this->error('提现服务不可用', 500);
}
$params = $this->get_json_params($request);
$amount = (float) ($params['amount'] ?? 0);
$method = sanitize_key((string) ($params['method'] ?? 'alipay'));
$account = sanitize_textarea_field((string) ($params['account_info'] ?? $params['account'] ?? ''));
$settings = class_exists('AKSRT_Settings') ? AKSRT_Settings::get_withdrawal_settings() : [];
$min = (float) ($settings['min_amount'] ?? 50);
if ($amount < $min) {
return $this->error(sprintf(__('最低提现金额为 %s', 'aksrt'), $min));
}
if ($account === '') {
return $this->error(__('请填写收款账户信息', 'aksrt'));
}
$user_id = get_current_user_id();
$balance = (float) AKSRT_Credits::balance($user_id);
if ($amount > $balance) {
return $this->error(__('可提现余额不足', 'aksrt'));
}
$cost = (int) round($amount);
if ($cost <= 0) {
return $this->error(__('提现金额无效', 'aksrt'));
}
$deducted = AKSRT_Credits::deduct($user_id, $cost, 'withdraw', ['type' => 'withdraw'], __('用户提现申请', 'aksrt'));
if (is_wp_error($deducted)) {
return $this->error($deducted->get_error_message());
}
$id = AKSRT_Withdrawal::get_instance()->create($user_id, $amount, $method, $account);
if ($id <= 0) {
AKSRT_Credits::add($user_id, $cost, 'withdraw_refund', ['type' => 'withdraw'], __('提现失败退回', 'aksrt'));
return $this->error(__('提现申请创建失败', 'aksrt'), 500);
}
return $this->success([
'message' => __('提现申请已提交', 'aksrt'),
'data' => [
'id' => $id,
'amount' => $amount,
'balance' => (float) AKSRT_Credits::balance($user_id),
],
], 201);
}
public function submit_frontend_product_review(WP_REST_Request $request): WP_REST_Response {
if (!class_exists('AKSRT_Product_Review')) {
return $this->error('评价服务不可用', 500);
}
$params = $this->get_json_params($request);
$order_number = sanitize_text_field((string) $request->get_param('order_number'));
$product_id = (int) ($params['product_id'] ?? 0);
$result = AKSRT_Product_Review::get_instance()->submit_review(
get_current_user_id(),
$order_number,
$product_id,
$params
);
if (empty($result['success'])) {
return $this->error($result['message'] ?? __('评价失败', 'aksrt'));
}
return $this->success([
'message' => __('评价已提交', 'aksrt'),
'data' => $result,
], 201);
}
public function create_frontend_product(WP_REST_Request $request): WP_REST_Response {
$params = $this->get_json_params($request);
$params['author_id'] = get_current_user_id();
@@ -333,6 +333,10 @@ trait AKSRT_Admin_Shop_Controller_Product_Operations {
update_post_meta($post_id, '_aksrt_product_image', $image);
}
if (isset($params['options']) && is_array($params['options']) && class_exists('AKSRT_Shop')) {
AKSRT_Shop::get_instance()->save_product_options((int) $post_id, $params['options']);
}
return $this->success([
'message' => __('商品已创建', 'aksrt'),
'data' => ['id' => $post_id],
@@ -484,6 +488,10 @@ trait AKSRT_Admin_Shop_Controller_Product_Operations {
update_post_meta($id, '_aksrt_product_image', esc_url_raw((string) $params['image']));
}
if (isset($params['options']) && is_array($params['options']) && class_exists('AKSRT_Shop')) {
AKSRT_Shop::get_instance()->save_product_options($id, $params['options']);
}
return $this->success([
'message' => __('商品已更新', 'aksrt'),
]);
@@ -352,7 +352,31 @@ class AKSRT_Admin_Shop_Controller extends AKSRT_Admin_Controller_Base {
'permission_callback' => [$this, 'check_logged_in_permission'],
]);
register_rest_route(self::API_NAMESPACE, '/shop/frontend/products/(?P<id>\d+)/favorite', [
register_rest_route(self::API_NAMESPACE, '/shop/frontend/products/archive', [
'methods' => 'GET',
'callback' => [$this, 'get_frontend_product_archive'],
'permission_callback' => '__return_true',
]);
register_rest_route(self::API_NAMESPACE, '/shop/frontend/revenue', [
'methods' => 'GET',
'callback' => [$this, 'get_frontend_revenue'],
'permission_callback' => [$this, 'check_logged_in_permission'],
]);
register_rest_route(self::API_NAMESPACE, '/shop/frontend/revenue/withdraw', [
'methods' => 'POST',
'callback' => [$this, 'create_frontend_withdraw'],
'permission_callback' => [$this, 'check_logged_in_permission'],
]);
register_rest_route(self::API_NAMESPACE, '/shop/frontend/orders/(?P<order_number>[A-Za-z0-9]+)/review', [
'methods' => 'POST',
'callback' => [$this, 'submit_frontend_product_review'],
'permission_callback' => [$this, 'check_logged_in_permission'],
]);
register_rest_route(self::API_NAMESPACE, '/shop/frontend/products/(?P<id>\d+)/favorite', [
'methods' => 'POST',
'callback' => [$this, 'toggle_frontend_product_favorite'],
'permission_callback' => [$this, 'check_logged_in_permission'],