From 825d86bb50f48f9a21d740d474c0dddee858dffb Mon Sep 17 00:00:00 2001 From: FIN-IT_AndriFP Date: Mon, 1 Dec 2025 11:31:30 +0700 Subject: (andri) show upsells product from magento in similarbottom product --- src/pages/api/magento-product.ts | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/pages/api/magento-product.ts b/src/pages/api/magento-product.ts index c906079e..c494b05d 100644 --- a/src/pages/api/magento-product.ts +++ b/src/pages/api/magento-product.ts @@ -1,3 +1,4 @@ +// pages/api/magento-product.ts import type { NextApiRequest, NextApiResponse } from 'next'; export default async function handler( @@ -48,7 +49,7 @@ export default async function handler( const data = await response.json(); // ===================================================================== - // TAMBAHAN: FETCH LABEL ATRIBUT (z_*) + // TAMBAHAN 1: FETCH LABEL ATRIBUT (z_*) // ===================================================================== let specsWithLabels: any[] = []; @@ -56,6 +57,7 @@ export default async function handler( // Cek apakah ada custom_attributes if (data.custom_attributes) { // Filter atribut yang kodenya dimulai dengan 'z' + // FIX: Menghapus filter 'Pakai link' agar MSDS tetap muncul const zAttributes = data.custom_attributes.filter((attr: any) => attr.attribute_code.startsWith('z') ); @@ -64,8 +66,8 @@ export default async function handler( specsWithLabels = await Promise.all( zAttributes.map(async (attr: any) => { try { - // Endpoint untuk ambil detail atribut (Label): /V1/products/attributes/{attributeCode} - const attrUrl = `${baseUrl}/attributes/${attr.attribute_code}`; + // Endpoint untuk ambil detail atribut (Label) + const attrUrl = `https://pimdev.1211.my.id/rest/V1/products/attributes/${attr.attribute_code}`; const attrRes = await fetch(attrUrl, { method: 'GET', @@ -77,8 +79,8 @@ export default async function handler( if (attrRes.ok) { const attrData = await attrRes.json(); - // AMBIL NILAI 'default_frontend_label' return { + code: attr.attribute_code, // FIX: Kirim code agar bisa dideteksi frontend (z_doc_) label: attrData.default_frontend_label || attr.attribute_code, value: attr.value }; @@ -87,11 +89,12 @@ export default async function handler( console.error(`Failed to fetch label for ${attr.attribute_code}`); } - // Fallback: Jika gagal ambil label, format manual dari kode + // Fallback: Format manual const fallbackLabel = attr.attribute_code - .substring(1).replace(/_/g, ' ').trim(); // z_size_ml -> size ml + .substring(1).replace(/_/g, ' ').trim(); return { + code: attr.attribute_code, // FIX: Kirim code di fallback juga label: fallbackLabel, value: attr.value }; @@ -99,10 +102,26 @@ export default async function handler( ); } - // Gabungkan data asli dengan data specs yang sudah ada labelnya + // ===================================================================== + // TAMBAHAN 2: AMBIL UP-SELLS (product_links) + // ===================================================================== + let upsellIds: number[] = []; + + if (data.product_links && Array.isArray(data.product_links)) { + upsellIds = data.product_links + // Filter hanya link type 'upsell' + .filter((link: any) => link.link_type === 'upsell') + // Ambil SKU (yang isinya ID Odoo) dan ubah ke number + .map((link: any) => Number(link.linked_product_sku)); + } + + // ===================================================================== + // RESPONSE GABUNGAN + // ===================================================================== const responseData = { ...data, - specs: specsWithLabels // Frontend tinggal pakai field ini + specs: specsWithLabels, // Data Spesifikasi (z_*) + upsell_ids: upsellIds // Data Upsell ID (product_links) }; res.status(200).json(responseData); -- cgit v1.2.3