summaryrefslogtreecommitdiff
path: root/src/pages
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-05-24 11:24:07 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-05-24 11:24:07 +0700
commit5b3e86c51c858e14cc7b9acc60ef0446fc5deaab (patch)
treede461ca3393bde2090694a3d66132fb8acbd3b3a /src/pages
parent3c020236bd95ba485cc7cfb8b11e5972c49bb7b5 (diff)
Feature generate google merchant product xml
Diffstat (limited to 'src/pages')
-rw-r--r--src/pages/google_merchant/products/[page].js68
-rw-r--r--src/pages/google_merchant/products/index.js35
2 files changed, 103 insertions, 0 deletions
diff --git a/src/pages/google_merchant/products/[page].js b/src/pages/google_merchant/products/[page].js
new file mode 100644
index 00000000..7db826bb
--- /dev/null
+++ b/src/pages/google_merchant/products/[page].js
@@ -0,0 +1,68 @@
+import { createSlug } from '@/core/utils/slug'
+import toTitleCase from '@/core/utils/toTitleCase'
+import productSearchApi from '@/lib/product/api/productSearchApi'
+import _ from 'lodash-contrib'
+import { create } from 'xmlbuilder'
+
+export async function getServerSideProps({ res, query }) {
+ const titleContent = 'Indoteknik.com: B2B Industrial Supply & Solution'
+ const descriptionContent =
+ 'Temukan pilihan produk B2B Industri & Alat Teknik untuk Perusahaan, UMKM & Pemerintah dengan lengkap, mudah dan transparan.'
+
+ const { page } = query
+ const limit = 5000
+ const queries = {
+ limit,
+ page: page.replace('.xml', ''),
+ priceFrom: 1,
+ orderBy: 'popular',
+ fq: 'image_s:["" TO *]'
+ }
+ const products = await productSearchApi({ query: _.toQuery(queries) })
+
+ const productItems = []
+ products.response.products.forEach((product) => {
+ const productUrl = createSlug('/shop/product/', product.name, product.id, true)
+ const productId = product.code != '' ? product.code : product.id
+ const productDescription = product.description?.replace(/<[^>]*>/g, ' ') || ''
+
+ const item = {
+ 'g:id': { '#text': productId },
+ 'g:title': { '#text': `<![CDATA[${toTitleCase(product.name)}]]>` },
+ 'g:description': { '#text': `<![CDATA[${productDescription}]]>` },
+ 'g:link': { '#text': productUrl },
+ 'g:image': { '#text': product.image },
+ 'g:condition': { '#text': 'new' },
+ 'g:availability': { '#text': 'in_stock' },
+ 'g:brand': { '#text': product.manufacture?.name || '' },
+ 'g:price': { '#text': `${product.lowestPrice.price} IDR` }
+ }
+ if (product.lowestPrice.discountPercentage > 0) {
+ item['g:sale_price'] = { '#text': `${product.lowestPrice.priceDiscount} IDR` }
+ }
+ productItems.push(item)
+ })
+
+ const googleMerchant = {
+ rss: {
+ '@xmlns:g': 'http://base.google.com/ns/1.0',
+ '@version': '2.0',
+ channel: {
+ title: { '#text': `<![CDATA[${titleContent}]]>` },
+ link: { '#text': process.env.SELF_HOST },
+ description: { '#text': `<![CDATA[${descriptionContent}]]>` },
+ item: productItems
+ }
+ }
+ }
+
+ res.setHeader('Content-Type', 'text/xml;charset=iso-8859-1')
+ res.write(create(googleMerchant).end())
+ res.end()
+
+ return { props: {} }
+}
+
+export default function GoogleMerchantPage() {
+ return null
+}
diff --git a/src/pages/google_merchant/products/index.js b/src/pages/google_merchant/products/index.js
new file mode 100644
index 00000000..38b37d29
--- /dev/null
+++ b/src/pages/google_merchant/products/index.js
@@ -0,0 +1,35 @@
+import productSearchApi from '@/lib/product/api/productSearchApi'
+import _ from 'lodash-contrib'
+
+export async function getServerSideProps() {
+ const limit = 5000
+ const queries = {
+ limit: 1,
+ priceFrom: 1,
+ fq: 'image_s:["" TO *]'
+ }
+ const products = await productSearchApi({ query: _.toQuery(queries) })
+ const pageTotal = Math.ceil(products.response.numFound / limit)
+
+ return { props: { pageTotal } }
+}
+
+export default function GoogleMerchantPage({ pageTotal }) {
+ const numberArray = Array.from({ length: pageTotal }, (_, index) => index)
+ return (
+ <div className='grid grid-cols-1 gap-y-3 p-4'>
+ <h1 className='text-h-lg font-semibold'>Google Merchant:</h1>
+ {numberArray.map((number) => (
+ <a
+ key={number}
+ href={`/google_merchant/products/${number + 1}.xml`}
+ className='block font-medium text-indigo-600'
+ target='_blank'
+ rel='noreferrer'
+ >
+ Page {number + 1}
+ </a>
+ ))}
+ </div>
+ )
+}