Skip to content

Commit 3dc1386

Browse files
committed
Update CartContents.component.tsx
1 parent 8225738 commit 3dc1386

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/components/Cart/CartContents.component.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const CartContents = () => {
4848
});
4949

5050
const handleRemoveProductClick = (cartKey: string, products: IProductRootObject[]) => {
51-
if (products.length) {
51+
if (products?.length) {
5252
const updatedItems = getUpdatedItems(products, 0, cartKey);
5353
updateCart({
5454
variables: {
@@ -71,10 +71,15 @@ const CartContents = () => {
7171

7272
const cartTotal = data?.cart?.total || '0';
7373

74+
const getUnitPrice = (subtotal: string, quantity: number) => {
75+
const numericSubtotal = parseFloat(subtotal.replace(/[^0-9.-]+/g,""));
76+
return isNaN(numericSubtotal) ? 'N/A' : (numericSubtotal / quantity).toFixed(2);
77+
};
78+
7479
return (
7580
<div className="container mx-auto px-4 py-8">
7681
<h1 className="text-3xl font-bold mb-8">Handlekurv</h1>
77-
{data?.cart?.contents?.nodes.length ? (
82+
{data?.cart?.contents?.nodes?.length ? (
7883
<>
7984
<div className="bg-white rounded-lg shadow-md p-6 mb-8">
8085
{data.cart.contents.nodes.map((item: IProductRootObject) => (
@@ -90,13 +95,8 @@ const CartContents = () => {
9095
</div>
9196
<div className="flex-grow ml-4">
9297
<h2 className="text-lg font-semibold">{item.product.node.name}</h2>
93-
{/* Display the price if available, otherwise show a placeholder */}
9498
<p className="text-gray-600">
95-
{item.product.node.priceHtml ? (
96-
<span dangerouslySetInnerHTML={{ __html: item.product.node.priceHtml }} />
97-
) : (
98-
'Price not available'
99-
)}
99+
Enhetspris: kr {getUnitPrice(item.subtotal, item.quantity)}
100100
</p>
101101
</div>
102102
<div className="flex items-center">

0 commit comments

Comments
 (0)