feat(shop): add product taxonomy write REST for editor
Expose POST /shop/products/taxonomy to create category/tag terms, enrich GET term payload with slug/parent/count, and let ProductEditorDrawer create categories and ensure tags without WP native UI.
This commit is contained in:
1 parent
e0b60fb3ea
commit
3d021d08f6
3 files changed
+198
-33
No files matched your search
@@ -45,14 +45,26 @@
|
|||||||
<n-divider title-placement="left">分类与标签</n-divider>
|
<n-divider title-placement="left">分类与标签</n-divider>
|
||||||
|
|
||||||
<n-form-item label="分类">
|
<n-form-item label="分类">
|
||||||
<n-select
|
<n-space vertical style="width:100%;">
|
||||||
v-model:value="form.categories"
|
<n-select
|
||||||
:options="categoryOptions"
|
v-model:value="form.categories"
|
||||||
multiple
|
:options="categoryOptions"
|
||||||
filterable
|
multiple
|
||||||
placeholder="选择分类(可多选)"
|
filterable
|
||||||
:loading="loadingTaxonomy"
|
placeholder="选择分类(可多选)"
|
||||||
/>
|
:loading="loadingTaxonomy"
|
||||||
|
style="width:100%;"
|
||||||
|
/>
|
||||||
|
<n-space>
|
||||||
|
<n-input
|
||||||
|
v-model:value="newCategoryName"
|
||||||
|
placeholder="新建分类名称"
|
||||||
|
style="min-width:180px;"
|
||||||
|
@keyup.enter="createCategory"
|
||||||
|
/>
|
||||||
|
<n-button :loading="creatingCategory" @click="createCategory">新建分类</n-button>
|
||||||
|
</n-space>
|
||||||
|
</n-space>
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
|
|
||||||
<n-form-item label="标签">
|
<n-form-item label="标签">
|
||||||
@@ -62,8 +74,9 @@
|
|||||||
multiple
|
multiple
|
||||||
filterable
|
filterable
|
||||||
tag
|
tag
|
||||||
placeholder="输入标签后回车"
|
placeholder="选择或输入新标签后回车"
|
||||||
:loading="loadingTaxonomy"
|
:loading="loadingTaxonomy"
|
||||||
|
@update:value="onTagsUpdate"
|
||||||
/>
|
/>
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
|
|
||||||
@@ -164,6 +177,9 @@ const shippingModeOptions = [
|
|||||||
|
|
||||||
const categoryOptions = ref([])
|
const categoryOptions = ref([])
|
||||||
const tagOptions = ref([])
|
const tagOptions = ref([])
|
||||||
|
const newCategoryName = ref('')
|
||||||
|
const creatingCategory = ref(false)
|
||||||
|
const creatingTagNames = new Set()
|
||||||
|
|
||||||
// Load taxonomy options
|
// Load taxonomy options
|
||||||
async function loadTaxonomy() {
|
async function loadTaxonomy() {
|
||||||
@@ -175,12 +191,75 @@ async function loadTaxonomy() {
|
|||||||
tagOptions.value = (res.data.tags || []).map(t => ({ label: t.name, value: t.name }))
|
tagOptions.value = (res.data.tags || []).map(t => ({ label: t.name, value: t.name }))
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// Taxonomy endpoint may not exist yet — silent fallback
|
message.warning('分类标签加载失败,可稍后重试')
|
||||||
} finally {
|
} finally {
|
||||||
loadingTaxonomy.value = false
|
loadingTaxonomy.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function createCategory() {
|
||||||
|
const name = (newCategoryName.value || '').trim()
|
||||||
|
if (!name) {
|
||||||
|
message.warning('请输入分类名称')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
creatingCategory.value = true
|
||||||
|
try {
|
||||||
|
const res = await api.request('/shop/products/taxonomy', {
|
||||||
|
method: 'POST',
|
||||||
|
data: { taxonomy: 'category', name },
|
||||||
|
})
|
||||||
|
const term = res?.data
|
||||||
|
if (term?.id) {
|
||||||
|
if (!categoryOptions.value.some(o => o.value === term.id)) {
|
||||||
|
categoryOptions.value = [...categoryOptions.value, { label: term.name, value: term.id }]
|
||||||
|
}
|
||||||
|
if (!form.categories.includes(term.id)) {
|
||||||
|
form.categories = [...form.categories, term.id]
|
||||||
|
}
|
||||||
|
newCategoryName.value = ''
|
||||||
|
message.success(term.existing ? '分类已存在,已选中' : '分类已创建')
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
message.error(err?.message || '创建分类失败')
|
||||||
|
} finally {
|
||||||
|
creatingCategory.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function ensureTagTerm(name) {
|
||||||
|
const label = String(name || '').trim()
|
||||||
|
if (!label || creatingTagNames.has(label)) return
|
||||||
|
const exists = tagOptions.value.some(o => o.value === label || o.label === label)
|
||||||
|
if (exists) return
|
||||||
|
|
||||||
|
creatingTagNames.add(label)
|
||||||
|
try {
|
||||||
|
const res = await api.request('/shop/products/taxonomy', {
|
||||||
|
method: 'POST',
|
||||||
|
data: { taxonomy: 'tag', name: label },
|
||||||
|
})
|
||||||
|
const term = res?.data
|
||||||
|
if (term?.name && !tagOptions.value.some(o => o.value === term.name)) {
|
||||||
|
tagOptions.value = [...tagOptions.value, { label: term.name, value: term.name }]
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// Keep typed tag in form; backend product save still accepts string tags.
|
||||||
|
} finally {
|
||||||
|
creatingTagNames.delete(label)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function onTagsUpdate(values) {
|
||||||
|
const list = Array.isArray(values) ? values : []
|
||||||
|
form.tags = list
|
||||||
|
list.forEach((name) => {
|
||||||
|
if (!tagOptions.value.some(o => o.value === name)) {
|
||||||
|
ensureTagTerm(name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Load product data when editing
|
// Load product data when editing
|
||||||
watch(() => props.show, async (val) => {
|
watch(() => props.show, async (val) => {
|
||||||
if (val) {
|
if (val) {
|
||||||
|
|||||||
@@ -47,35 +47,114 @@ trait AKSRT_Admin_Shop_Controller_Product_Operations {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function get_product_taxonomy(): WP_REST_Response {
|
public function get_product_taxonomy(): WP_REST_Response {
|
||||||
$format_terms = static function (string $taxonomy): array {
|
|
||||||
if (!taxonomy_exists($taxonomy)) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
$terms = get_terms([
|
|
||||||
'taxonomy' => $taxonomy,
|
|
||||||
'hide_empty' => false,
|
|
||||||
]);
|
|
||||||
if (is_wp_error($terms)) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
return array_map(static function (WP_Term $term): array {
|
|
||||||
return [
|
|
||||||
'id' => $term->term_id,
|
|
||||||
'name' => $term->name,
|
|
||||||
];
|
|
||||||
}, $terms);
|
|
||||||
};
|
|
||||||
|
|
||||||
return $this->success([
|
return $this->success([
|
||||||
'data' => [
|
'data' => [
|
||||||
'categories' => $format_terms('aksrt_product_cat'),
|
'categories' => $this->format_product_terms('aksrt_product_cat'),
|
||||||
'tags' => $format_terms('aksrt_product_tag'),
|
'tags' => $this->format_product_terms('aksrt_product_tag'),
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* POST /shop/products/taxonomy — create category or tag term.
|
||||||
|
*
|
||||||
|
* Body: { taxonomy: 'category'|'tag'|'aksrt_product_cat'|'aksrt_product_tag', name: string, parent?: int }
|
||||||
|
*/
|
||||||
|
public function create_product_taxonomy_term(WP_REST_Request $request): WP_REST_Response {
|
||||||
|
$params = $this->get_json_params($request);
|
||||||
|
$raw_tax = sanitize_key((string) ($params['taxonomy'] ?? 'category'));
|
||||||
|
$name = sanitize_text_field((string) ($params['name'] ?? ''));
|
||||||
|
$parent = max(0, (int) ($params['parent'] ?? 0));
|
||||||
|
|
||||||
|
$taxonomy_map = [
|
||||||
|
'category' => 'aksrt_product_cat',
|
||||||
|
'tag' => 'aksrt_product_tag',
|
||||||
|
'aksrt_product_cat' => 'aksrt_product_cat',
|
||||||
|
'aksrt_product_tag' => 'aksrt_product_tag',
|
||||||
|
];
|
||||||
|
$taxonomy = $taxonomy_map[$raw_tax] ?? '';
|
||||||
|
|
||||||
|
if ($taxonomy === '' || !taxonomy_exists($taxonomy)) {
|
||||||
|
return $this->error(__('无效的分类法', 'aksrt'));
|
||||||
|
}
|
||||||
|
if ($name === '') {
|
||||||
|
return $this->error(__('名称不能为空', 'aksrt'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$existing = term_exists($name, $taxonomy, $taxonomy === 'aksrt_product_cat' ? $parent : 0);
|
||||||
|
if (is_array($existing) && !empty($existing['term_id'])) {
|
||||||
|
$term = get_term((int) $existing['term_id'], $taxonomy);
|
||||||
|
if ($term && !is_wp_error($term)) {
|
||||||
|
return $this->success([
|
||||||
|
'message' => __('术语已存在', 'aksrt'),
|
||||||
|
'data' => [
|
||||||
|
'id' => (int) $term->term_id,
|
||||||
|
'name' => (string) $term->name,
|
||||||
|
'slug' => (string) $term->slug,
|
||||||
|
'parent' => (int) $term->parent,
|
||||||
|
'taxonomy' => $taxonomy === 'aksrt_product_cat' ? 'category' : 'tag',
|
||||||
|
'existing' => true,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$args = [];
|
||||||
|
if ($taxonomy === 'aksrt_product_cat' && $parent > 0) {
|
||||||
|
$args['parent'] = $parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = wp_insert_term($name, $taxonomy, $args);
|
||||||
|
if (is_wp_error($result)) {
|
||||||
|
return $this->error($result->get_error_message());
|
||||||
|
}
|
||||||
|
|
||||||
|
$term_id = (int) ($result['term_id'] ?? 0);
|
||||||
|
$term = get_term($term_id, $taxonomy);
|
||||||
|
if (!$term || is_wp_error($term)) {
|
||||||
|
return $this->error(__('创建失败', 'aksrt'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->success([
|
||||||
|
'message' => __('术语已创建', 'aksrt'),
|
||||||
|
'data' => [
|
||||||
|
'id' => (int) $term->term_id,
|
||||||
|
'name' => (string) $term->name,
|
||||||
|
'slug' => (string) $term->slug,
|
||||||
|
'parent' => (int) $term->parent,
|
||||||
|
'taxonomy' => $taxonomy === 'aksrt_product_cat' ? 'category' : 'tag',
|
||||||
|
'existing' => false,
|
||||||
|
],
|
||||||
|
], 201);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array{id:int,name:string,slug:string,parent:int,count:int}>
|
||||||
|
*/
|
||||||
|
private function format_product_terms(string $taxonomy): array {
|
||||||
|
if (!taxonomy_exists($taxonomy)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$terms = get_terms([
|
||||||
|
'taxonomy' => $taxonomy,
|
||||||
|
'hide_empty' => false,
|
||||||
|
]);
|
||||||
|
if (is_wp_error($terms) || !is_array($terms)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_map(static function (WP_Term $term): array {
|
||||||
|
return [
|
||||||
|
'id' => (int) $term->term_id,
|
||||||
|
'name' => (string) $term->name,
|
||||||
|
'slug' => (string) $term->slug,
|
||||||
|
'parent' => (int) $term->parent,
|
||||||
|
'count' => (int) $term->count,
|
||||||
|
];
|
||||||
|
}, $terms);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET /shop/products — 商品列表,支持 search、status、page、per_page
|
* GET /shop/products — 商品列表,支持 search、status、page、per_page
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -175,6 +175,13 @@ class AKSRT_Admin_Shop_Controller extends AKSRT_Admin_Controller_Base {
|
|||||||
'permission_callback' => [$this, 'check_permission'],
|
'permission_callback' => [$this, 'check_permission'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// Create product category or tag term for the product editor.
|
||||||
|
register_rest_route(self::API_NAMESPACE, '/shop/products/taxonomy', [
|
||||||
|
'methods' => 'POST',
|
||||||
|
'callback' => [$this, 'create_product_taxonomy_term'],
|
||||||
|
'permission_callback' => [$this, 'check_permission'],
|
||||||
|
]);
|
||||||
|
|
||||||
// Product list
|
// Product list
|
||||||
register_rest_route(self::API_NAMESPACE, '/shop/products', [
|
register_rest_route(self::API_NAMESPACE, '/shop/products', [
|
||||||
'methods' => 'GET',
|
'methods' => 'GET',
|
||||||
|
|||||||
Reference in new issue
Block a user