product page: get selected variant price instead of main product price
This commit is contained in:
@@ -22,7 +22,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.quantityButton {
|
.quantityButton {
|
||||||
padding: 0px;
|
padding: 0;
|
||||||
width: 25px;
|
width: 25px;
|
||||||
height: 32px;
|
height: 32px;
|
||||||
display: flex;
|
display: flex;
|
||||||
@@ -41,7 +41,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.removeButton {
|
.removeButton {
|
||||||
padding: 4px 0px;
|
padding: 4px 0;
|
||||||
height: 32px;
|
height: 32px;
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|||||||
@@ -7,13 +7,13 @@ import { useMemo, useState } from "react";
|
|||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useAppDispatch, useAppSelector } from "redux/hooks";
|
import { useAppDispatch, useAppSelector } from "redux/hooks";
|
||||||
import { colors, ProBlack2 } from "ThemeConstants";
|
import { colors, ProBlack2 } from "ThemeConstants";
|
||||||
import { Product } from "utils/types/appTypes";
|
import { Product, Variant } from "utils/types/appTypes";
|
||||||
import styles from "../product.module.css";
|
import styles from "../product.module.css";
|
||||||
|
|
||||||
export default function ProductFooter({
|
export default function ProductFooter({
|
||||||
product,
|
product,
|
||||||
isValid = true,
|
isValid = true,
|
||||||
variantId,
|
selectedVariant,
|
||||||
selectedExtras,
|
selectedExtras,
|
||||||
selectedGroups,
|
selectedGroups,
|
||||||
quantity,
|
quantity,
|
||||||
@@ -21,7 +21,7 @@ export default function ProductFooter({
|
|||||||
}: {
|
}: {
|
||||||
product: Product;
|
product: Product;
|
||||||
isValid?: boolean;
|
isValid?: boolean;
|
||||||
variantId: string;
|
selectedVariant?: Variant;
|
||||||
selectedExtras: string[];
|
selectedExtras: string[];
|
||||||
selectedGroups: string[];
|
selectedGroups: string[];
|
||||||
quantity: number;
|
quantity: number;
|
||||||
@@ -60,11 +60,11 @@ export default function ProductFooter({
|
|||||||
item: {
|
item: {
|
||||||
id: product?.id,
|
id: product?.id,
|
||||||
name: product?.name,
|
name: product?.name,
|
||||||
price: product?.price,
|
price: selectedVariant?.price || product?.price,
|
||||||
image: product?.image,
|
image: product?.image,
|
||||||
description: product?.description,
|
description: product?.description,
|
||||||
comment: specialRequest,
|
comment: specialRequest,
|
||||||
variant: variantId,
|
variant: selectedVariant?.id,
|
||||||
extras: selectedExtras,
|
extras: selectedExtras,
|
||||||
extrasgroup: selectedGroups,
|
extrasgroup: selectedGroups,
|
||||||
isHasLoyalty: product?.isHasLoyalty,
|
isHasLoyalty: product?.isHasLoyalty,
|
||||||
|
|||||||
@@ -88,34 +88,32 @@ export default function ProductDetailPage({
|
|||||||
}, [product?.variants]);
|
}, [product?.variants]);
|
||||||
|
|
||||||
// Get the final selected variant ID (the variant that matches all current selections)
|
// Get the final selected variant ID (the variant that matches all current selections)
|
||||||
const getFinalSelectedVariantId = useCallback(() => {
|
const getFinalSelectedVariant = useCallback(() => {
|
||||||
if (!product?.variants || Object.keys(selectedVariants).length === 0)
|
if (!product?.variants || Object.keys(selectedVariants).length === 0)
|
||||||
return "";
|
return undefined;
|
||||||
|
|
||||||
// Find the variant that matches all current selections
|
// Find the variant that matches all current selections
|
||||||
const matchingVariant = product.variants.find((variant) => {
|
// Convert to string only if defined, otherwise return empty string
|
||||||
|
return product.variants.find((variant) => {
|
||||||
return Object.entries(selectedVariants).every(([level, value]) => {
|
return Object.entries(selectedVariants).every(([level, value]) => {
|
||||||
const levelNum = parseInt(level);
|
const levelNum = parseInt(level);
|
||||||
return variant.options[levelNum]?.value === value;
|
return variant.options[levelNum]?.value === value;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Convert to string only if defined, otherwise return empty string
|
|
||||||
return matchingVariant?.id?.toString() || "";
|
|
||||||
}, [product?.variants, selectedVariants]);
|
}, [product?.variants, selectedVariants]);
|
||||||
|
|
||||||
const getExtras = useCallback(() => {
|
const getExtras = useCallback(() => {
|
||||||
const finalSelectedVariantId = getFinalSelectedVariantId();
|
const finalSelectedVariant = getFinalSelectedVariant();
|
||||||
if (finalSelectedVariantId) {
|
if (finalSelectedVariant) {
|
||||||
const variant = product?.variants?.find(
|
const variant = product?.variants?.find(
|
||||||
(variant) => variant.id === Number(finalSelectedVariantId),
|
(variant) => variant.id === finalSelectedVariant.id,
|
||||||
);
|
);
|
||||||
if (variant?.extras?.length && variant.extras.length > 0) {
|
if (variant?.extras?.length && variant.extras.length > 0) {
|
||||||
return variant.extras;
|
return variant.extras;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return product?.extras;
|
return product?.extras;
|
||||||
}, [product?.variants, product?.extras, getFinalSelectedVariantId]);
|
}, [product?.variants, product?.extras, getFinalSelectedVariant]);
|
||||||
|
|
||||||
// Validation function to check if all required selections are made
|
// Validation function to check if all required selections are made
|
||||||
const validateRequiredSelections = () => {
|
const validateRequiredSelections = () => {
|
||||||
@@ -277,7 +275,7 @@ export default function ProductDetailPage({
|
|||||||
<ProductFooter
|
<ProductFooter
|
||||||
product={product}
|
product={product}
|
||||||
isValid={isValid}
|
isValid={isValid}
|
||||||
variantId={getFinalSelectedVariantId()}
|
selectedVariant={getFinalSelectedVariant()}
|
||||||
selectedExtras={selectedExtras}
|
selectedExtras={selectedExtras}
|
||||||
selectedGroups={Object.values(selectedExtrasByGroup).flat()}
|
selectedGroups={Object.values(selectedExtrasByGroup).flat()}
|
||||||
quantity={quantity}
|
quantity={quantity}
|
||||||
@@ -326,7 +324,7 @@ export default function ProductDetailPage({
|
|||||||
<ProductFooter
|
<ProductFooter
|
||||||
product={product}
|
product={product}
|
||||||
isValid={isValid}
|
isValid={isValid}
|
||||||
variantId={getFinalSelectedVariantId()}
|
selectedVariant={getFinalSelectedVariant()}
|
||||||
selectedExtras={selectedExtras}
|
selectedExtras={selectedExtras}
|
||||||
selectedGroups={Object.values(selectedExtrasByGroup).flat()}
|
selectedGroups={Object.values(selectedExtrasByGroup).flat()}
|
||||||
quantity={quantity}
|
quantity={quantity}
|
||||||
|
|||||||
@@ -201,7 +201,6 @@ export type FilterColumn =
|
|||||||
| "created_at"
|
| "created_at"
|
||||||
| "warehouse_id"
|
| "warehouse_id"
|
||||||
| "filters[send_datetime][$eq]"
|
| "filters[send_datetime][$eq]"
|
||||||
| "warehouse_id"
|
|
||||||
| "account_id"
|
| "account_id"
|
||||||
| "customer_id"
|
| "customer_id"
|
||||||
| "currency_id"
|
| "currency_id"
|
||||||
@@ -350,12 +349,12 @@ export interface ApiResponse<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface DiscountResultType {
|
export interface DiscountResultType {
|
||||||
value: number
|
value: number;
|
||||||
is_on_category: boolean
|
is_on_category: boolean;
|
||||||
id: number
|
id: number;
|
||||||
isDiscount: boolean
|
isDiscount: boolean;
|
||||||
isGift: boolean
|
isGift: boolean;
|
||||||
categories: any[]
|
categories: any[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Restaurant {
|
export interface Restaurant {
|
||||||
@@ -565,14 +564,14 @@ export interface Product {
|
|||||||
theExtrasGroups: TheExtrasGroup[];
|
theExtrasGroups: TheExtrasGroup[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Variant {
|
export type Variant = {
|
||||||
id: number;
|
id: string;
|
||||||
price: number;
|
price: number;
|
||||||
available: string;
|
available: string;
|
||||||
options: Option[];
|
options: Option[];
|
||||||
optionsAR: Option[];
|
optionsAR: Option[];
|
||||||
extras: Extra[];
|
extras: Extra[];
|
||||||
}
|
};
|
||||||
|
|
||||||
export interface Option {
|
export interface Option {
|
||||||
option: string;
|
option: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user