diff --git a/lib/actions/index.ts b/lib/actions/index.ts index 3b1a06e..c680827 100644 --- a/lib/actions/index.ts +++ b/lib/actions/index.ts @@ -71,4 +71,20 @@ export async function getAllProducts() { } catch (error) { console.log(error); } +} +export async function getSimilarProducts(productId: string) { + try { + connectToDB(); + + const currentProduct = await Product.findById(productId); + + if (!currentProduct) return null; + + const similarProducts = await Product.find({ _id: { $ne: productId } }).limit(3); + + return similarProducts + + } catch (error) { + console.log(error); + } } \ No newline at end of file