Skip to content

Commit 5ed4824

Browse files
committed
Grouped Product out of stock
1 parent de0c2e9 commit 5ed4824

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

components/Product/ProductTypes/GroupedProduct/GroupedProduct.tsx

+37-29
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ export const GroupedProduct: FunctionComponent<GroupedProductProps> = ({ group }
4646
async ({ items }) => {
4747
if (!cartId || addingSimpleProductsToCart.loading) return
4848

49-
await addSimpleProductToCart(items.filter((item: any) => item.data.quantity > 0))
49+
const values = items.filter((item: any) => item.data.quantity > 0)
50+
51+
if (values.length === 0) return
52+
53+
await addSimpleProductToCart(values)
5054

5155
await history.push('/cart')
5256

@@ -57,34 +61,38 @@ export const GroupedProduct: FunctionComponent<GroupedProductProps> = ({ group }
5761

5862
return (
5963
<Root as={Form} onSubmit={handleAddToCart}>
60-
{items?.map(({ sku, name, price, stock, quantity }, key) => (
61-
<Item key={key}>
62-
<Title>{name}</Title>
63-
64-
<Input type="hidden" name={`items[${key}].data.sku`} value={sku} rules={{ required: true }} />
65-
66-
<PriceContainer>
67-
<Price
68-
label={price.maximum.regular.value > price.minimum.regular.value ? 'Starting at' : undefined}
69-
regular={price.minimum.regular.value}
70-
special={price.minimum.discount.amountOff && price.minimum.final.value - price.minimum.discount.amountOff}
71-
currency={price.minimum.regular.currency}
72-
/>
73-
74-
<Quantity
75-
name={`items[${key}].data.quantity`}
76-
defaultValue={stock === 'IN_STOCK' ? quantity : 0}
77-
disabled={stock === 'IN_STOCK'}
78-
addLabel="Add"
79-
removeLabel="Remove"
80-
minValue={0}
81-
min={0}
82-
rules={{ required: true }}
83-
hideError
84-
/>
85-
</PriceContainer>
86-
</Item>
87-
))}
64+
{items?.map(({ sku, name, price, stock, quantity }, key) => {
65+
const inStock = stock === 'IN_STOCK'
66+
67+
return (
68+
<Item key={key}>
69+
<Title>{name}</Title>
70+
71+
<Input type="hidden" name={`items[${key}].data.sku`} value={sku} rules={{ required: true }} />
72+
73+
<PriceContainer>
74+
<Price
75+
label={price.maximum.regular.value > price.minimum.regular.value ? 'Starting at' : undefined}
76+
regular={price.minimum.regular.value}
77+
special={price.minimum.discount.amountOff && price.minimum.final.value - price.minimum.discount.amountOff}
78+
currency={price.minimum.regular.currency}
79+
/>
80+
81+
<Quantity
82+
name={`items[${key}].data.quantity`}
83+
defaultValue={inStock ? quantity : 0}
84+
disabled={!inStock}
85+
addLabel="Add"
86+
removeLabel="Remove"
87+
minValue={0}
88+
min={0}
89+
rules={{ required: true }}
90+
hideError
91+
/>
92+
</PriceContainer>
93+
</Item>
94+
)
95+
})}
8896
<Button type="submit" as="button" text="Add to Cart" loading={addingSimpleProductsToCart.loading} />
8997
</Root>
9098
)

0 commit comments

Comments
 (0)