diff options
Diffstat (limited to 'src/pages')
| -rw-r--r-- | src/pages/api/shop/search.js | 15 | ||||
| -rw-r--r-- | src/pages/google_merchant/products/[page].js | 45 |
2 files changed, 51 insertions, 9 deletions
diff --git a/src/pages/api/shop/search.js b/src/pages/api/shop/search.js index fd7a215a..b4d67c5d 100644 --- a/src/pages/api/shop/search.js +++ b/src/pages/api/shop/search.js @@ -13,20 +13,20 @@ export default async function handler(req, res) { orderBy = '', operation = 'AND', fq = '', - limit = 0, + limit = 30, stock = '' } = req.query let paramOrderBy = '' switch (orderBy) { case 'price-asc': - paramOrderBy += 'price_discount_f ASC' + paramOrderBy += 'price_tier1_v2_f ASC' break case 'price-desc': - paramOrderBy += 'price_discount_f DESC' + paramOrderBy += 'price_tier1_v2_f DESC' break case 'popular': - paramOrderBy += 'search_rank_i DESC' + paramOrderBy += 'product_rating_f DESC, search_rank_i DESC,' break case 'popular-weekly': paramOrderBy += 'search_rank_weekly_i DESC' @@ -34,6 +34,9 @@ export default async function handler(req, res) { case 'stock': paramOrderBy += 'stock_total_f DESC' break + case 'flashsale-price-asc': + paramOrderBy += 'flashsale_price_f ASC' + break default: paramOrderBy += 'product_rating_f DESC, price_discount_f DESC' break @@ -41,7 +44,7 @@ export default async function handler(req, res) { let offset = (page - 1) * limit let parameter = [ - 'facet.field=manufacture_name', + 'facet.field=manufacture_name_s', 'facet.field=category_name', 'facet=true', 'indent=true', @@ -49,7 +52,7 @@ export default async function handler(req, res) { `q.op=${operation}`, `q=${escapeSolrQuery(q)}`, 'qf=name_s', - `start=${offset}`, + `start=${parseInt(offset)}`, `rows=${limit}`, `sort=${paramOrderBy}`, `fq=-publish_b:false` diff --git a/src/pages/google_merchant/products/[page].js b/src/pages/google_merchant/products/[page].js index d6309090..c8b4079b 100644 --- a/src/pages/google_merchant/products/[page].js +++ b/src/pages/google_merchant/products/[page].js @@ -2,6 +2,7 @@ import { createSlug } from '@/core/utils/slug' import toTitleCase from '@/core/utils/toTitleCase' import productSearchApi from '@/lib/product/api/productSearchApi' import variantSearchApi from '@/lib/product/api/variantSearchApi' +import axios from 'axios' import _ from 'lodash-contrib' import { create } from 'xmlbuilder' @@ -21,8 +22,11 @@ export async function getServerSideProps({ res, query }) { } const products = await variantSearchApi({ query: _.toQuery(queries) }) + const brandsData = {} + const categoriesData = {} + const productItems = [] - products.response.products.forEach((product) => { + for (const product of products.response.products) { const productUrl = createSlug('/shop/product/variant/', product.name, product.id, true) const productId = product.code != '' ? product.code : product.id const regexHtmlTags = /(<([^>]+)>)/gi @@ -34,6 +38,27 @@ export async function getServerSideProps({ res, query }) { product.description = defaultProductDescription } + let categoryName = null + + let brandId = product.manufacture?.id ?? null + let categoryId = null + + if (brandId && brandId in brandsData) { + categoryId = brandsData[brandId].category_ids?.[0] ?? null + } else { + const solrBrand = await getBrandById(brandId) + brandsData[brandId] = solrBrand + categoryId = solrBrand?.category_ids?.[0] ?? null + } + + if (categoryId && categoryId in categoriesData) { + categoryName = categoriesData[categoryId].name_s ?? null + } else { + const solrCategory = await getCategoryById(categoryId) + categoriesData[categoryId] = solrCategory + categoryName = solrCategory?.name_s ?? null + } + const availability = 'in_stock' const item = { @@ -47,20 +72,24 @@ export async function getServerSideProps({ res, query }) { 'g:brand': { '#text': product.manufacture?.name || '' }, 'g:price': { '#text': `${Math.round(product.lowestPrice.price * 1.11)} IDR` } } - + if (product.stockTotal == 0) { item['g:custom_label_0'] = { '#text': 'Stok Tidak Tersedia' } } else { item['g:custom_label_1'] = { '#text': 'Stok Tersedia' } } + if (categoryName) { + item['g:custom_label_2'] = { '#text': categoryName } + } + if (product.lowestPrice.discountPercentage > 0) { item['g:sale_price'] = { '#text': `${Math.round(product.lowestPrice.priceDiscount * 1.11)} IDR` } } productItems.push(item) - }) + } const googleMerchant = { rss: { @@ -82,6 +111,16 @@ export async function getServerSideProps({ res, query }) { return { props: {} } } +const getBrandById = async (id) => { + const brand = await axios(`${process.env.SOLR_HOST}/solr/brands/select?q=id:${id}`) + return brand.data.response.docs[0] ?? null +} + +const getCategoryById = async (id) => { + const category = await axios(`${process.env.SOLR_HOST}/solr/categories/select?q=id:${id}`) + return category.data.response.docs[0] ?? null +} + export default function GoogleMerchantPage() { return null } |
