Skip to content

Commit 82ba124

Browse files
committed
feat: 옵션 가격까지 포함하여 0원인 상품인 경우 장바구니에 담거나 바로 결제가 가능하도록 수정
1 parent 4f49b10 commit 82ba124

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

packages/shop/src/components/features/product.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ type ProductItemPropType = {
9393
startPurchaseProcess: (oneItemOrderData: ShopSchemas.CartItemAppendRequest) => void;
9494
};
9595

96+
/**
97+
* 상품 가격과 옵션 가격을 합산하여 상품의 총 가격이 0인지 확인합니다.
98+
* @param p 상품 객체
99+
* @returns 상품의 가격이 0 이하인 경우 true, 그렇지 않으면 false
100+
*/
101+
const isZeroPriceProduct = (p: ShopSchemas.Product): boolean => {
102+
return p.price + p.option_groups.reduce((sum, group) => sum + group.options.reduce((s, o) => s + (o.additional_price || 0), 0), 0) === 0;
103+
};
104+
96105
const ProductItem: React.FC<ProductItemPropType> = ({ disabled: rootDisabled, language, product, startPurchaseProcess, onAddToCartSuccess }) => {
97106
const navigate = useNavigate();
98107
const [, forceRender] = React.useReducer((x) => x + 1, 0);
@@ -210,7 +219,7 @@ const ProductItem: React.FC<ProductItemPropType> = ({ disabled: rootDisabled, la
210219

211220
const addItemToCart = () => {
212221
const formData = getCartAppendRequestPayload(product, getValues());
213-
if (getTotalProductPrice(getValues()) <= 0) {
222+
if (!isZeroPriceProduct(product) && getTotalProductPrice(getValues()) <= 0) {
214223
alert(cannotAddToCartZeroPriceProductStr);
215224
return;
216225
}
@@ -238,7 +247,7 @@ const ProductItem: React.FC<ProductItemPropType> = ({ disabled: rootDisabled, la
238247
};
239248
const onOrderOneItemButtonClick = () => {
240249
const formData = getCartAppendRequestPayload(product, getValues());
241-
if (getTotalProductPrice(getValues()) <= 0) {
250+
if (!isZeroPriceProduct(product) && getTotalProductPrice(getValues()) <= 0) {
242251
alert(cannotPurchaseZeroPriceProductStr);
243252
return;
244253
}

0 commit comments

Comments
 (0)