Files
wordpress-theme-aksrt/inc/classes/admin/class-aksrt-admin-shop-controller-variant-export-operations.php
T

112 lines
4.2 KiB
PHP

<?php
/**
* AKSRT admin shop variant and export operations.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
trait AKSRT_Admin_Shop_Controller_Variant_Export_Operations {
public function get_product_variants(WP_REST_Request $request): WP_REST_Response {
$product_id = (int) $request->get_param('id');
$product = get_post($product_id);
if (!$product || $product->post_type !== 'aksrt_product') {
return $this->error('商品不存在', 404);
}
if (!class_exists('AKSRT_Shop')) {
return $this->success(['data' => ['variants' => []]]);
}
$variants = AKSRT_Shop::get_instance()->get_variants($product_id);
return $this->success(['data' => ['variants' => $variants]]);
}
public function save_product_variants(WP_REST_Request $request): WP_REST_Response {
$product_id = (int) $request->get_param('id');
$product = get_post($product_id);
if (!$product || $product->post_type !== 'aksrt_product') {
return $this->error('商品不存在', 404);
}
$params = $request->get_json_params() ?? [];
$variants = $params['variants'] ?? [];
if (!is_array($variants)) {
return $this->error('变体数据格式错误');
}
if (!class_exists('AKSRT_Shop')) {
return $this->error('商城系统未启用', 501);
}
$saved = AKSRT_Shop::get_instance()->save_variants($product_id, $variants);
return $this->success(['message' => '变体已保存', 'data' => ['variants' => $saved]]);
}
public function delete_variant(WP_REST_Request $request): WP_REST_Response {
$variant_id = (int) $request->get_param('id');
if (!class_exists('AKSRT_Shop')) {
return $this->error('商城系统未启用', 501);
}
$result = AKSRT_Shop::get_instance()->delete_variant($variant_id);
if (!$result) {
return $this->error('删除失败', 500);
}
return $this->success(['message' => '变体已删除']);
}
public function export_orders(WP_REST_Request $request): WP_REST_Response {
if (!class_exists('AKSRT_Shop')) {
return $this->error('商城服务不可用', 500);
}
$args = [
'status' => sanitize_key((string) $request->get_param('status')),
'date_from' => sanitize_text_field((string) $request->get_param('date_from')),
'date_to' => sanitize_text_field((string) $request->get_param('date_to')),
'limit' => max(1, min(5000, (int) ($request->get_param('limit') ?: 1000))),
];
$result = AKSRT_Shop::get_instance()->export_orders_csv($args);
return new WP_REST_Response($result['csv'], 200, [
'Content-Type' => 'text/csv; charset=utf-8',
'Content-Disposition' => 'attachment; filename="orders-' . date('Ymd-His') . '.csv"',
'X-Order-Count' => $result['count'],
]);
}
public function confirm_receipt(WP_REST_Request $request): WP_REST_Response {
if (!class_exists('AKSRT_Shop')) {
return $this->error('商城服务不可用', 500);
}
$order_number = sanitize_text_field((string) $request->get_param('order_number'));
$result = AKSRT_Shop::get_instance()->confirm_receipt(get_current_user_id(), $order_number);
if (empty($result['success'])) {
return $this->error($result['message'] ?? '确认收货失败');
}
return $this->success(['data' => $result['data']]);
}
public function get_after_sale_reasons(WP_REST_Request $request): WP_REST_Response {
if (class_exists('AKSRT_After_Sale')) {
return $this->success(['data' => AKSRT_After_Sale::get_reason_options()]);
}
return $this->success(['data' => ['refund' => [], 'return' => []]]);
}
public function get_frontend_after_sale_reasons(WP_REST_Request $request): WP_REST_Response {
if (class_exists('AKSRT_After_Sale')) {
return $this->success(['data' => AKSRT_After_Sale::get_reason_options()]);
}
return $this->success(['data' => ['refund' => [], 'return' => []]]);
}
}