From de958dcdf3487befab11630466cad23344bfc8fb Mon Sep 17 00:00:00 2001 From: Wijayaac Date: Thu, 28 Mar 2024 10:53:35 +0700 Subject: [PATCH] Add : discount api fetching --- components/carousel.tsx | 2 +- components/cart/actions.ts | 36 ++++++++++++---------- components/cart/modal.tsx | 2 +- components/grid/three-items.tsx | 2 +- components/layout/footer.tsx | 6 ++-- components/layout/navbar/index.tsx | 2 +- lib/shopify/index.ts | 8 +++-- lib/shopify/queries/discount-metaobject.ts | 6 ++-- lib/shopify/types.ts | 6 ++-- lib/utils.ts | 23 ++++++++++++++ 10 files changed, 60 insertions(+), 33 deletions(-) diff --git a/components/carousel.tsx b/components/carousel.tsx index f190140ee..5153efa0c 100644 --- a/components/carousel.tsx +++ b/components/carousel.tsx @@ -4,7 +4,7 @@ import { GridTileImage } from './grid/tile'; export async function Carousel() { // Collections that start with `hidden-*` are hidden from the search page. - const products = await getCollectionProducts({ collection: 'boxes' }); + const products = await getCollectionProducts({ collection: 'all' }); if (!products?.length) return null; diff --git a/components/cart/actions.ts b/components/cart/actions.ts index 982193349..9dfc1a2c9 100644 --- a/components/cart/actions.ts +++ b/components/cart/actions.ts @@ -1,7 +1,15 @@ 'use server'; import { TAGS } from 'lib/constants'; -import { addToCart, createCart, getCart, removeFromCart, updateCart } from 'lib/shopify'; +import { + addToCart, + createCart, + getCart, + getDiscountMetaobjects, + removeFromCart, + updateCart +} from 'lib/shopify'; +import { formatDiscounts } from 'lib/utils'; import { revalidateTag } from 'next/cache'; import { cookies } from 'next/headers'; @@ -83,22 +91,13 @@ export async function updateItemQuantity( } export async function calculateDiscounts(cart: any) { - const discountGroups = [ - { - name: 'Tier 2', - discount: { - amount: 0.1, - minimumSpent: 150 - } - }, - { - name: 'Tier 1', - discount: { - amount: 0.05, - minimumSpent: 120 - } - } - ]; + const metaobjects = await getDiscountMetaobjects('dynamic_discount'); + + const discountGroups = formatDiscounts(metaobjects); + + if (discountGroups === undefined) { + return; + } const subTotal = cart?.cost.subtotalAmount.amount; const currencyCode = cart?.cost.subtotalAmount.currencyCode; @@ -122,12 +121,15 @@ export async function calculateDiscounts(cart: any) { : 0; const nextDiscount = closestNextTier?.discount.amount; const discountAmount = finalDiscount ? finalDiscount * 100 : 0; + // get discount code from the final discount group + const discountCode = eligibleDiscount.length ? eligibleDiscount[0]?.code : ''; return { discountAmount, spentToNextDiscount, nextDiscount, discountGroups: discountGroupsSorted, + discountCode, minSpent, subTotal, currencyCode diff --git a/components/cart/modal.tsx b/components/cart/modal.tsx index 83d319a68..5956f1bbe 100644 --- a/components/cart/modal.tsx +++ b/components/cart/modal.tsx @@ -182,7 +182,7 @@ export default function CartModal({ Proceed to Checkout diff --git a/components/grid/three-items.tsx b/components/grid/three-items.tsx index 86953f226..38e841f63 100644 --- a/components/grid/three-items.tsx +++ b/components/grid/three-items.tsx @@ -40,7 +40,7 @@ function ThreeItemGridItem({ export async function ThreeItemGrid() { // Collections that start with `hidden-*` are hidden from the search page. const homepageItems = await getCollectionProducts({ - collection: 'sale' + collection: 'all' }); if (!homepageItems[0] || !homepageItems[1] || !homepageItems[2]) return null; diff --git a/components/layout/footer.tsx b/components/layout/footer.tsx index 6c60e6959..15fc67dd7 100644 --- a/components/layout/footer.tsx +++ b/components/layout/footer.tsx @@ -1,9 +1,9 @@ import Link from 'next/link'; -import FooterMenu from 'components/layout/footer-menu'; import LogoSquare from 'components/logo-square'; import { getMenu } from 'lib/shopify'; import { Suspense } from 'react'; +import FooterMenu from './footer-menu'; const { COMPANY_NAME, SITE_NAME } = process.env; @@ -16,9 +16,9 @@ export default async function Footer() { return (