Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Replace taxonomies/taxons with categories across admin API and frontend

Damian Legawiec41f034586fad
diff --git a/packages/admin-sdk/src/admin-client.ts b/packages/admin-sdk/src/admin-client.ts
index bf74e1c04c..e934ff2fe0 100644
--- a/packages/admin-sdk/src/admin-client.ts
+++ b/packages/admin-sdk/src/admin-client.ts
@@ -1,7 +1,7 @@
import type { RequestFn, RequestOptions, AuthTokens, LoginCredentials } from '@spree/sdk-core';
import { transformListParams, getParams } from '@spree/sdk-core';
import type { PaginatedResponse, ListParams } from '@spree/sdk-core';
-import type { Store, Product, Order, Asset, Taxon, TaxCategory, ShippingCategory } from './types';
+import type { Store, Product, Order, Asset, Category, TaxCategory, ShippingCategory } from './types';
import type {
StoreUpdateParams,
ProductUpdateParams,
@@ -154,12 +154,12 @@ export class AdminClient {
};
// ============================================
- // Taxons
+ // Categories
// ============================================
- readonly taxons = {
- list: (params?: ListParams & Record<string, unknown>, options?: RequestOptions): Promise<PaginatedResponse<Taxon>> =>
- this.request<PaginatedResponse<Taxon>>('GET', '/taxons', {
+ readonly categories = {
+ list: (params?: ListParams & Record<string, unknown>, options?: RequestOptions): Promise<PaginatedResponse<Category>> =>
+ this.request<PaginatedResponse<Category>>('GET', '/categories', {
...options,
params: params ? transformListParams(params) : undefined,
}),
diff --git a/packages/admin-sdk/src/params.ts b/packages/admin-sdk/src/params.ts
index 86c796d018..3c4f8c481f 100644
--- a/packages/admin-sdk/src/params.ts
+++ b/packages/admin-sdk/src/params.ts
@@ -151,7 +151,7 @@ export interface ProductCreateParams {
sku?: string;
shipping_category_id: string;
tax_category_id?: string;
- taxon_ids?: Array<string>;
+ category_ids?: Array<string>;
tags?: Array<string>;
variants?: Array<{
sku?: string;
@@ -176,7 +176,7 @@ export interface ProductUpdateParams {
sku?: string;
shipping_category_id?: string;
tax_category_id?: string;
- taxon_ids?: Array<string>;
+ category_ids?: Array<string>;
tags?: Array<string>;
variants?: Array<{
sku?: string;
@@ -192,17 +192,7 @@ export interface ProductUpdateParams {
}>;
}
-export interface TaxonomyCreateParams {
- name: string;
- position?: number;
-}
-
-export interface TaxonomyUpdateParams {
- name?: string;
- position?: number;
-}
-
-export interface TaxonCreateParams {
+export interface CategoryCreateParams {
name: string;
parent_id?: string;
position?: number;
@@ -215,7 +205,7 @@ export interface TaxonCreateParams {
sort_order?: string;
}
-export interface TaxonUpdateParams {
+export interface CategoryUpdateParams {
name?: string;
parent_id?: string;
position?: number;
diff --git a/packages/admin-sdk/src/types/index.ts b/packages/admin-sdk/src/types/index.ts
index 025fb5dd69..fefc723d92 100644
--- a/packages/admin-sdk/src/types/index.ts
+++ b/packages/admin-sdk/src/types/index.ts
@@ -17,4 +17,4 @@ export type { default as Product } from './generated/Product';
export type { default as ShippingCategory } from './generated/ShippingCategory';
export type { default as Store } from './generated/Store';
export type { default as TaxCategory } from './generated/TaxCategory';
-export type { default as Taxon } from './generated/Taxon';
+export type { default as Category } from './generated/Category';
diff --git a/packages/admin/src/components/app-sidebar.tsx b/packages/admin/src/components/app-sidebar.tsx
index 8f5cb60730..98b4b5e681 100644
--- a/packages/admin/src/components/app-sidebar.tsx
+++ b/packages/admin/src/components/app-sidebar.tsx
@@ -40,7 +40,7 @@ const navigation: NavItem[] = [
items: [
{ title: 'Price Lists', url: '/products/price-lists' },
{ title: 'Stock', url: '/products/stock' },
- { title: 'Taxonomies', url: '/products/taxonomies' },
+ { title: 'Categories', url: '/products/categories' },
{ title: 'Options', url: '/products/options' },
],
},
diff --git a/packages/admin/src/hooks/use-taxons.ts b/packages/admin/src/hooks/use-categories.ts
similarity index 56%
rename from packages/admin/src/hooks/use-taxons.ts
rename to packages/admin/src/hooks/use-categories.ts
index f62c1bdb3d..f6538ec350 100644
--- a/packages/admin/src/hooks/use-taxons.ts
+++ b/packages/admin/src/hooks/use-categories.ts
@@ -1,11 +1,11 @@
import { useQuery } from '@tanstack/react-query'
import { adminClient } from '@/client'
-export function useTaxons() {
+export function useCategories() {
return useQuery({
- queryKey: ['taxons'],
+ queryKey: ['categories'],
queryFn: () =>
- adminClient.taxons.list({ limit: 100, expand: ['taxonomy'] }),
+ adminClient.categories.list({ limit: 100 }),
staleTime: 1000 * 60 * 5,
})
}
diff --git a/packages/admin/src/hooks/use-product.ts b/packages/admin/src/hooks/use-product.ts
index 68918b9ba9..0cc6d87212 100644
--- a/packages/admin/src/hooks/use-product.ts
+++ b/packages/admin/src/hooks/use-product.ts
@@ -10,7 +10,7 @@ export function useProduct(id: string) {
'variants',
'images',
'option_types',
- 'taxons',
+ 'categories',
'shipping_category',
'tax_category',
],
diff --git a/packages/admin/src/routes/_authenticated/$storeId/products/$productId.tsx b/packages/admin/src/routes/_authenticated/$storeId/products/$productId.tsx
index 76613c5693..f617d781dc 100644
--- a/packages/admin/src/routes/_authenticated/$storeId/products/$productId.tsx
+++ b/packages/admin/src/routes/_authenticated/$storeId/products/$productId.tsx
@@ -64,7 +64,7 @@ import {
} from '@/hooks/use-product-assets'
import { useShippingCategories } from '@/hooks/use-shipping-categories'
import { useTaxCategories } from '@/hooks/use-tax-categories'
-import { useTaxons } from '@/hooks/use-taxons'
+import { useCategories } from '@/hooks/use-categories'
import { productFormSchema, type ProductFormValues } from '@/schemas/product'
export const Route = createFileRoute('/_authenticated/$storeId/products/$productId')({
@@ -85,7 +85,7 @@ function productToFormValues(product: Product): ProductFormValues {
make_active_at: product.make_active_at ?? null,
available_on: product.available_on ?? null,
discontinue_on: product.discontinue_on ?? null,
- taxon_ids: product.taxons?.map((t) => t.id) ?? [],
+ category_ids: product.categories?.map((t) => t.id) ?? [],
tags: product.tags ?? [],
price: master?.price?.amount ? Number(master.price.amount) : undefined,
compare_at_price: master?.original_price?.amount
@@ -704,8 +704,8 @@ function StatusCard({ form }: FormCardProps) {
// ---------------------------------------------------------------------------
function CategorizationCard({ form }: FormCardProps) {
- const { data: taxonsResponse } = useTaxons()
- const taxons = taxonsResponse?.data ?? []
+ const { data: categoriesResponse } = useCategories()
+ const categories = categoriesResponse?.data ?? []
return (
<Card>
@@ -716,11 +716,11 @@ function CategorizationCard({ form }: FormCardProps) {
<div className="grid gap-2">
<Label>Categories</Label>
<Controller
- name="taxon_ids"
+ name="category_ids"
control={form.control}
render={({ field }) => (
- <TaxonCombobox
- taxons={taxons}
+ <CategoryCombobox
+ categories={categories}
value={field.value ?? []}
onChange={field.onChange}
/>
@@ -746,17 +746,17 @@ function CategorizationCard({ form }: FormCardProps) {
)
}
-interface TaxonOption {
+interface CategoryOption {
id: string
name: string
}
-function TaxonCombobox({
- taxons,
+function CategoryCombobox({
+ categories,
value,
onChange,
}: {
- taxons: TaxonOption[]
+ categories: CategoryOption[]
value: string[]
onChange: (value: string[]) => void
}) {
@@ -773,7 +773,7 @@ function TaxonCombobox({
{(selectedValues: string[]) =>
selectedValues.map((id) => (
<ComboboxChip key={id}>
- {taxons.find((t) => t.id === id)?.name ?? id}
+ {categories.find((c) => c.id === id)?.name ?? id}
</ComboboxChip>
))
}
@@ -782,9 +782,9 @@ function TaxonCombobox({
</ComboboxChips>
<ComboboxContent anchor={anchorRef}>
<ComboboxList>
- {taxons.map((taxon) => (
- <ComboboxItem key={taxon.id} value={taxon.id}>
- {taxon.name}
+ {categories.map((category) => (
+ <ComboboxItem key={category.id} value={category.id}>
+ {category.name}
</ComboboxItem>
))}
<ComboboxEmpty>No categories found</ComboboxEmpty>
diff --git a/packages/admin/src/routes/_authenticated/products/$productId.tsx b/packages/admin/src/routes/_authenticated/products/$productId.tsx
index 8e63007797..0574102a4e 100644
--- a/packages/admin/src/routes/_authenticated/products/$productId.tsx
+++ b/packages/admin/src/routes/_authenticated/products/$productId.tsx
@@ -50,7 +50,7 @@ import {
} from '@/hooks/use-product-assets'
import { useShippingCategories } from '@/hooks/use-shipping-categories'
import { useTaxCategories } from '@/hooks/use-tax-categories'
-import { useTaxons } from '@/hooks/use-taxons'
+import { useCategories } from '@/hooks/use-categories'
import { productFormSchema, type ProductFormValues } from '@/schemas/product'
export const Route = createFileRoute('/_authenticated/products/$productId')({
@@ -71,7 +71,7 @@ function productToFormValues(product: Product): ProductFormValues {
make_active_at: product.make_active_at ?? null,
available_on: product.available_on ?? null,
discontinue_on: product.discontinue_on ?? null,
- taxon_ids: product.taxons?.map((t) => t.id) ?? [],
+ category_ids: product.categories?.map((t) => t.id) ?? [],
tags: product.tags ?? [],
price: master?.price?.amount ? Number(master.price.amount) : undefined,
compare_at_price: master?.original_price?.amount
@@ -689,8 +689,8 @@ function StatusCard({ form }: FormCardProps) {
// ---------------------------------------------------------------------------
function CategorizationCard({ form }: FormCardProps) {
- const { data: taxonsResponse } = useTaxons()
- const taxons = taxonsResponse?.data ?? []
+ const { data: categoriesResponse } = useCategories()
+ const categories = categoriesResponse?.data ?? []
return (
<Card>
@@ -701,11 +701,11 @@ function CategorizationCard({ form }: FormCardProps) {
<div className="grid gap-2">
<Label>Categories</Label>
<Controller
- name="taxon_ids"
+ name="category_ids"
control={form.control}
render={({ field }) => (
- <TaxonCombobox
- taxons={taxons}
+ <CategoryCombobox
+ categories={categories}
value={field.value ?? []}
onChange={field.onChange}
/>
@@ -731,17 +731,17 @@ function CategorizationCard({ form }: FormCardProps) {
)
}
-interface TaxonOption {
+interface CategoryOption {
id: string
name: string
}
-function TaxonCombobox({
- taxons,
+function CategoryCombobox({
+ categories,
value,
onChange,
}: {
- taxons: TaxonOption[]
+ categories: CategoryOption[]
value: string[]
onChange: (value: string[]) => void
}) {
@@ -758,7 +758,7 @@ function TaxonCombobox({
{(selectedValues: string[]) =>
selectedValues.map((id) => (
<ComboboxChip key={id}>
- {taxons.find((t) => t.id === id)?.name ?? id}
+ {categories.find((c) => c.id === id)?.name ?? id}
</ComboboxChip>
))
}
@@ -767,9 +767,9 @@ function TaxonCombobox({
</ComboboxChips>
<ComboboxContent anchor={anchorRef}>
<ComboboxList>
- {taxons.map((taxon) => (
- <ComboboxItem key={taxon.id} value={taxon.id}>
- {taxon.name}
+ {categories.map((category) => (
+ <ComboboxItem key={category.id} value={category.id}>
+ {category.name}
</ComboboxItem>
))}
<ComboboxEmpty>No categories found</ComboboxEmpty>
diff --git a/packages/admin/src/schemas/product.ts b/packages/admin/src/schemas/product.ts
index f9bdec98ff..3512333816 100644
--- a/packages/admin/src/schemas/product.ts
+++ b/packages/admin/src/schemas/product.ts
@@ -12,7 +12,7 @@ export const productFormSchema = z.object({
discontinue_on: z.string().nullable().optional(),
// Categorization
- taxon_ids: z.array(z.string()).optional(),
+ category_ids: z.array(z.string()).optional(),
tags: z.array(z.string()).optional(),
// Pricing (master variant)
diff --git a/spree/api/app/controllers/spree/api/v3/admin/categories_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/categories_controller.rb
new file mode 100644
index 0000000000..e32c1d548d
--- /dev/null
+++ b/spree/api/app/controllers/spree/api/v3/admin/categories_controller.rb
@@ -0,0 +1,23 @@
+module Spree
+ module Api
+ module V3
+ module Admin
+ class CategoriesController < ResourceController
+ protected
+
+ def model_class
+ Spree::Category
+ end
+
+ def serializer_class
+ Spree.api.admin_category_serializer
+ end
+
+ def scope
+ super.where(taxonomy: current_store.taxonomies)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/taxonomies/taxons_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/taxonomies/taxons_controller.rb
deleted file mode 100644
index 54b3c95827..0000000000
--- a/spree/api/app/controllers/spree/api/v3/admin/taxonomies/taxons_controller.rb
+++ /dev/null
@@ -1,30 +0,0 @@
-module Spree
- module Api
- module V3
- module Admin
- module Taxonomies
- class TaxonsController < ResourceController
- protected
-
- def model_class
- Spree::Taxon
- end
-
- def serializer_class
- Spree.api.admin_taxon_serializer
- end
-
- def set_parent
- @parent = current_store.taxonomies.find_by_prefix_id!(params[:taxonomy_id])
- authorize!(:show, @parent)
- end
-
- def parent_association
- :taxons
- end
- end
- end
- end
- end
- end
-end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/taxonomies_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/taxonomies_controller.rb
deleted file mode 100644
index 060a353e33..0000000000
--- a/spree/api/app/controllers/spree/api/v3/admin/taxonomies_controller.rb
+++ /dev/null
@@ -1,19 +0,0 @@
-module Spree
- module Api
- module V3
- module Admin
- class TaxonomiesController < ResourceController
- protected
-
- def model_class
- Spree::Taxonomy
- end
-
- def serializer_class
- Spree.api.admin_taxonomy_serializer
- end
- end
- end
- end
- end
-end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/taxons_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/taxons_controller.rb
deleted file mode 100644
index ec39dd0757..0000000000
--- a/spree/api/app/controllers/spree/api/v3/admin/taxons_controller.rb
+++ /dev/null
@@ -1,19 +0,0 @@
-module Spree
- module Api
- module V3
- module Admin
- class TaxonsController < ResourceController
- protected
-
- def model_class
- Spree::Taxon
- end
-
- def serializer_class
- Spree.api.admin_taxon_serializer
- end
- end
- end
- end
- end
-end
diff --git a/spree/api/config/routes.rb b/spree/api/config/routes.rb
index 60cc06a34f..c0ec99183c 100644
--- a/spree/api/config/routes.rb
+++ b/spree/api/config/routes.rb
@@ -112,13 +112,8 @@ Spree::Core::Engine.add_routes do
resources :assets, controller: 'assets', only: [:index, :create, :update, :destroy]
end
- # Taxonomies > Taxons
- resources :taxonomies do
- resources :taxons, controller: 'taxonomies/taxons'
- end
-
- # Taxons (flat, top-level)
- resources :taxons, only: [:index, :show]
+ # Categories
+ resources :categories, only: [:index, :show]
# Option Types (with nested option_values in payload)
resources :option_types
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/taxonomies/taxons_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/taxonomies/taxons_controller_spec.rb
deleted file mode 100644
index 43f6b0e731..0000000000
--- a/spree/api/spec/controllers/spree/api/v3/admin/taxonomies/taxons_controller_spec.rb
+++ /dev/null
@@ -1,84 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Api::V3::Admin::Taxonomies::TaxonsController, type: :controller do
- render_views
-
- include_context 'API v3 Admin authenticated'
-
- let!(:taxonomy) { create(:taxonomy, store: store) }
- let!(:taxon) { create(:taxon, taxonomy: taxonomy) }
-
- before { request.headers.merge!(headers) }
-
- describe 'GET #index' do
- it 'returns taxons for the taxonomy' do
- get :index, params: { taxonomy_id: taxonomy.prefixed_id }, as: :json
-
- expect(response).to have_http_status(:ok)
- expect(json_response['data']).to be_an(Array)
- ids = json_response['data'].map { |t| t['id'] }
- expect(ids).to include(taxon.prefixed_id)
- end
-
- context 'with taxonomy from another store' do
- let(:other_taxonomy) { create(:taxonomy, store: create(:store)) }
-
- it 'returns 404' do
- get :index, params: { taxonomy_id: other_taxonomy.prefixed_id }, as: :json
- expect(response).to have_http_status(:not_found)
- end
- end
- end
-
- describe 'GET #show' do
- it 'returns the taxon' do
- get :show, params: { taxonomy_id: taxonomy.prefixed_id, id: taxon.prefixed_id }, as: :json
-
- expect(response).to have_http_status(:ok)
- expect(json_response['id']).to eq(taxon.prefixed_id)
- expect(json_response['name']).to eq(taxon.name)
- end
- end
-
- describe 'POST #create' do
- it 'creates a taxon under the taxonomy' do
- expect {
- post :create, params: {
- taxonomy_id: taxonomy.prefixed_id,
- name: 'New Taxon',
- parent_id: taxonomy.root.id
- }, as: :json
- }.to change(taxonomy.taxons, :count).by(1)
-
- expect(response).to have_http_status(:created)
- expect(json_response['name']).to eq('New Taxon')
- end
- end
-
- describe 'PATCH #update' do
- it 'updates the taxon' do
- patch :update, params: {
- taxonomy_id: taxonomy.prefixed_id,
- id: taxon.prefixed_id,
- name: 'Updated Taxon'
- }, as: :json
-
- expect(response).to have_http_status(:ok)
- expect(json_response['name']).to eq('Updated Taxon')
- expect(taxon.reload.name).to eq('Updated Taxon')
- end
- end
-
- describe 'DELETE #destroy' do
- it 'deletes the taxon' do
- expect {
- delete :destroy, params: {
- taxonomy_id: taxonomy.prefixed_id,
- id: taxon.prefixed_id
- }, as: :json
- }.to change(taxonomy.taxons, :count).by(-1)
-
- expect(response).to have_http_status(:no_content)
- end
- end
-end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/taxonomies_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/taxonomies_controller_spec.rb
deleted file mode 100644
index 36721a82ee..0000000000
--- a/spree/api/spec/controllers/spree/api/v3/admin/taxonomies_controller_spec.rb
+++ /dev/null
@@ -1,83 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Api::V3::Admin::TaxonomiesController, type: :controller do
- render_views
-
- include_context 'API v3 Admin authenticated'
-
- let!(:taxonomy) { create(:taxonomy, store: store) }
-
- before { request.headers.merge!(headers) }
-
- describe 'GET #index' do
- it 'returns taxonomies for the current store' do
- get :index, as: :json
-
- expect(response).to have_http_status(:ok)
- expect(json_response['data']).to be_an(Array)
- ids = json_response['data'].map { |t| t['id'] }
- expect(ids).to include(taxonomy.prefixed_id)
- end
-
- it 'does not return taxonomies from other stores' do
- other_taxonomy = create(:taxonomy, store: create(:store))
-
- get :index, as: :json
-
- ids = json_response['data'].map { |t| t['id'] }
- expect(ids).not_to include(other_taxonomy.prefixed_id)
- end
- end
-
- describe 'GET #show' do
- it 'returns the taxonomy' do
- get :show, params: { id: taxonomy.prefixed_id }, as: :json
-
- expect(response).to have_http_status(:ok)
- expect(json_response['id']).to eq(taxonomy.prefixed_id)
- expect(json_response['name']).to eq(taxonomy.name)
- end
-
- it 'returns 404 for non-existent taxonomy' do
- get :show, params: { id: 'tax_nonexistent' }, as: :json
- expect(response).to have_http_status(:not_found)
- end
- end
-
- describe 'POST #create' do
- it 'creates a taxonomy' do
- post :create, params: { name: 'New Taxonomy' }, as: :json
-
- expect(response).to have_http_status(:created)
- expect(json_response['name']).to eq('New Taxonomy')
- end
-
- context 'with invalid params' do
- it 'returns validation errors' do
- post :create, params: { name: '' }, as: :json
-
- expect(response).to have_http_status(:unprocessable_content)
- end
- end
- end
-
- describe 'PATCH #update' do
- it 'updates the taxonomy' do
- patch :update, params: { id: taxonomy.prefixed_id, name: 'Updated Name' }, as: :json
-
- expect(response).to have_http_status(:ok)
- expect(json_response['name']).to eq('Updated Name')
- expect(taxonomy.reload.name).to eq('Updated Name')
- end
- end
-
- describe 'DELETE #destroy' do
- it 'deletes the taxonomy' do
- expect {
- delete :destroy, params: { id: taxonomy.prefixed_id }, as: :json
- }.to change(Spree::Taxonomy, :count).by(-1)
-
- expect(response).to have_http_status(:no_content)
- end
- end
-end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/taxons_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/taxons_controller_spec.rb
deleted file mode 100644
index 2ae809029a..0000000000
--- a/spree/api/spec/controllers/spree/api/v3/admin/taxons_controller_spec.rb
+++ /dev/null
@@ -1,38 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Api::V3::Admin::TaxonsController, type: :controller do
- render_views
-
- include_context 'API v3 Admin authenticated'
-
- let!(:taxonomy) { create(:taxonomy, store: store) }
- let!(:taxon) { create(:taxon, taxonomy: taxonomy) }
-
- before { request.headers.merge!(headers) }
-
- describe 'GET #index' do
- it 'returns all taxons' do
- get :index, as: :json
-
- expect(response).to have_http_status(:ok)
- expect(json_response['data']).to be_an(Array)
- ids = json_response['data'].map { |t| t['id'] }
- expect(ids).to include(taxon.prefixed_id)
- end
- end
-
- describe 'GET #show' do
- it 'returns the taxon' do
- get :show, params: { id: taxon.prefixed_id }, as: :json
-
- expect(response).to have_http_status(:ok)
- expect(json_response['id']).to eq(taxon.prefixed_id)
- expect(json_response['name']).to eq(taxon.name)
- end
-
- it 'returns 404 for non-existent taxon' do
- get :show, params: { id: 'taxon_nonexistent' }, as: :json
- expect(response).to have_http_status(:not_found)
- end
- end
-end