diff --git a/packs/vtex/loaders/legacy/productListingPage.ts b/packs/vtex/loaders/legacy/productListingPage.ts index 02786552..354ca35e 100644 --- a/packs/vtex/loaders/legacy/productListingPage.ts +++ b/packs/vtex/loaders/legacy/productListingPage.ts @@ -22,7 +22,8 @@ import { toProduct, } from "deco-sites/std/packs/vtex/utils/transform.ts"; import { fetchAPI, fetchSafe } from "deco-sites/std/utils/fetchVTEX.ts"; -import type { LegacyFacets, LegacyProduct } from "../../types.ts"; +import type { LegacyFacet, LegacyFacets, LegacyProduct } from "../../types.ts"; + import { withIsSimilarTo } from "../../utils/similars.ts"; const MAX_ALLOWED_PAGES = 500; @@ -136,6 +137,7 @@ const loader = async ( const _to = `${(page + 1) * count - 1}`; const pageTypes = await pageTypesFromPathname(maybeTerm, ctx); + const pageType = pageTypes.at(-1) || pageTypes[0]; if (pageTypes.length === 0 && !ft && !fq) { return null; @@ -182,8 +184,33 @@ const loader = async ( props.similars ? withIsSimilarTo(ctx, product) : product ), ); + + // Get categories of the current department/category + const getCategoryFacets = (CategoriesTrees: LegacyFacet[]): LegacyFacet[] => { + const isDepartmentOrCategoryPage = !pageType; + if (isDepartmentOrCategoryPage) { + return []; + } + + for (const category of CategoriesTrees) { + const isCurrentCategory = category.Id == Number(pageType.id); + if (isCurrentCategory) { + return category.Children || []; + } else if (category.Children.length) { + const childFacets = getCategoryFacets(category.Children); + const hasChildFacets = childFacets.length; + if (hasChildFacets) { + return childFacets; + } + } + } + + return []; + }; + const filters = Object.entries({ Departments: vtexFacets.Departments, + Categories: getCategoryFacets(vtexFacets.CategoriesTrees), Brands: vtexFacets.Brands, ...vtexFacets.SpecificationFilters, }).map(([name, facets]) => diff --git a/packs/vtex/types.ts b/packs/vtex/types.ts index 9215fdb1..34dc0b63 100644 --- a/packs/vtex/types.ts +++ b/packs/vtex/types.ts @@ -661,6 +661,7 @@ export type LegacyProduct = IProduct & { export type LegacyFacets = { Departments: LegacyFacet[]; + CategoriesTrees: LegacyFacet[]; Brands: LegacyFacet[]; SpecificationFilters: Record; }; @@ -695,6 +696,7 @@ export interface Category { } export interface LegacyFacet { + Id: number; Quantity: number; Name: string; Link: string; diff --git a/packs/vtex/utils/transform.ts b/packs/vtex/utils/transform.ts index 8121daed..e3554432 100644 --- a/packs/vtex/utils/transform.ts +++ b/packs/vtex/utils/transform.ts @@ -447,11 +447,6 @@ export const legacyFacetToFilter = ( const pathSet = new Set(pathSegments); const getLink = (facet: LegacyFacet, selected: boolean) => { - // Do not allow removing root facet to avoid going back to home page - if (mapSegments.length === 1) { - return `${url.pathname}${url.search}`; - } - const index = pathSegments.findIndex((s) => s === facet.Value); const newMap = selected ? [...mapSegments.filter((_, i) => i !== index)]