#how can I filter products by tags ?
14 messages · Page 1 of 1 (latest)
You could use the storefront api.
Use products.list() or the admin api products and it's calls and filter on tag IDs?
For this I created a service using medusa-extender. Happy to share the code if you need
Would love to see your code and maybe use it 🙂
Curious on your approach!
There are a few ways to do this - if you use meilisearch - the approach is different - if not - you can use the api and I will share my code with you np
`import { BaseService } from "medusa-interfaces";
import { Service } from "medusa-extender";
import { EntityManager, Repository } from "typeorm";
import { ProductTag } from "@medusajs/medusa";
type InjectedDependencies = {
manager: EntityManager;
};
@Service()
export class TagService extends BaseService {
static resolutionKey = "tagService";
private readonly manager: EntityManager;
private readonly productTagRepository: Repository<ProductTag>;
constructor(
{ manager }: InjectedDependencies,
private readonly config: any
) {
super();
this.manager = manager;
this.productTagRepository = this.manager.getRepository(ProductTag);
}
findTags(params: { value: string }) {
return this.productTagRepository.find({
where: params,
});
}
}
`
service
`import { MedusaAuthenticatedRequest, Router } from "medusa-extender";
import { Response, NextFunction } from "express";
import { User } from "@medusajs/medusa/dist";
import {TagService} from "./tag.service";
@Router({
routes: [
{
requiredAuth: false,
path: "/api/tags",
method: "get",
handlers: [
async (
req: MedusaAuthenticatedRequest,
res: Response,
next: NextFunction
): Promise<Response<User[]>> => {
const tagService = req.scope.resolve(
"tagService"
) as TagService;
if(typeof req.query.value === "string") {
const tags = await tagService.findTags({
value: req.query.value,
});
return res.send({tags});
}
},
],
},
],
})
export class TagRouter {}
`
Router
`import { Module } from "medusa-extender";
import { TagRouter } from "./tag.router";
import { TagService } from "./tag.service";
@Module({
imports: [TagRouter, TagService],
})
export class TagModule {}
`
module
Will check it tomorrow. 22:17 over here atm. Thanks @hidden pike !