Skip to content

Commit 9aa1411

Browse files
committed
Add cart endpoints, handle woo session
1 parent 11df5af commit 9aa1411

File tree

15 files changed

+296
-117
lines changed

15 files changed

+296
-117
lines changed

lib/api.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const API_URL = `${process.env.WORDPRESS_API_URL}`;
1+
const API_URL = `${process.env.NEXT_PUBLIC_WORDPRESS_API_URL}`;
22

33
export async function fetchAPI(query: string, options?: { variables: object }) {
44
const headers = { "Content-Type": "application/json" };
@@ -9,6 +9,11 @@ export async function fetchAPI(query: string, options?: { variables: object }) {
99
] = `Bearer ${process.env.WORDPRESS_AUTH_REFRESH_TOKEN}`;
1010
}
1111

12+
const session = process.browser ? localStorage.getItem("woo-session") : null;
13+
if (session) {
14+
console.log(session);
15+
headers["woocommerce-session"] = `Session ${session}`;
16+
}
1217
const { variables } = options ?? {};
1318

1419
const res = await fetch(API_URL, {
@@ -45,4 +50,3 @@ export async function getPreviewPost(id: string, idType = "DATABASE_ID") {
4550
);
4651
return data.post;
4752
}
48-

lib/clientGql/add-to-cart.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { fetchAPI } from "lib";
2+
import { AddToCartInput } from "lib/graphql";
3+
4+
export async function addToCart(input: AddToCartInput) {
5+
const data = await fetchAPI(
6+
`
7+
mutation AddToCart($input: AddToCartInput!) {
8+
addToCart(input: $input) {
9+
cartItem {
10+
key
11+
product {
12+
id
13+
name
14+
description
15+
type
16+
onSale
17+
slug
18+
averageRating
19+
reviewCount
20+
image {
21+
id
22+
sourceUrl
23+
altText
24+
}
25+
galleryImages {
26+
nodes {
27+
id
28+
sourceUrl
29+
altText
30+
}
31+
}
32+
}
33+
variation {
34+
id
35+
name
36+
description
37+
type
38+
onSale
39+
price
40+
regularPrice
41+
salePrice
42+
image {
43+
id
44+
sourceUrl
45+
altText
46+
}
47+
attributes {
48+
nodes {
49+
id
50+
attributeId
51+
name
52+
value
53+
}
54+
}
55+
}
56+
quantity
57+
total
58+
subtotal
59+
subtotalTax
60+
}
61+
}
62+
}
63+
`,
64+
{
65+
variables: {
66+
input,
67+
},
68+
}
69+
);
70+
71+
return data;
72+
}

lib/clientGql/get-cart.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { fetchAPI } from "lib";
2+
3+
export async function getCart() {
4+
const data = await fetchAPI(
5+
`
6+
query GetCart {
7+
cart {
8+
contents {
9+
nodes {
10+
key
11+
product {
12+
id
13+
name
14+
description
15+
type
16+
onSale
17+
slug
18+
averageRating
19+
reviewCount
20+
image {
21+
sourceUrl
22+
srcSet
23+
altText
24+
}
25+
}
26+
variation {
27+
id
28+
name
29+
description
30+
type
31+
onSale
32+
price
33+
regularPrice
34+
salePrice
35+
image {
36+
id
37+
sourceUrl
38+
srcSet
39+
altText
40+
title
41+
}
42+
attributes {
43+
nodes {
44+
id
45+
name
46+
value
47+
}
48+
}
49+
}
50+
quantity
51+
total
52+
subtotal
53+
subtotalTax
54+
}
55+
}
56+
appliedCoupons {
57+
nodes {
58+
id
59+
discountType
60+
amount
61+
dateExpiry
62+
products {
63+
nodes {
64+
id
65+
}
66+
}
67+
productCategories {
68+
nodes {
69+
id
70+
}
71+
}
72+
}
73+
}
74+
subtotal
75+
subtotalTax
76+
shippingTax
77+
shippingTotal
78+
total
79+
totalTax
80+
feeTax
81+
feeTotal
82+
discountTax
83+
discountTotal
84+
}
85+
}
86+
`
87+
);
88+
89+
return data;
90+
}

lib/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ export * from "./queries/products";
99
export * from "./queries/home";
1010
export * from "./queries/news";
1111
export * from "./queries/page-by-slug";
12+
13+
// Ecommerce
14+
export * from "./clientGql/get-cart";
15+
export * from "./clientGql/add-to-cart";

lib/queries/get-cart.ts

Lines changed: 0 additions & 101 deletions
This file was deleted.

lib/queries/product-by-slug.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export async function getProductBySlug(slug: string) {
77
fragment ProductFields on Product {
88
name
99
slug
10+
databaseId
1011
seo {
1112
canonical
1213
title
@@ -54,6 +55,17 @@ export async function getProductBySlug(slug: string) {
5455
shortDescription
5556
type
5657
...ProductFields
58+
productElements {
59+
productGallery {
60+
image {
61+
id
62+
sourceUrl
63+
srcSet
64+
altText
65+
title
66+
}
67+
}
68+
}
5769
productTypes {
5870
edges {
5971
node {

0 commit comments

Comments
 (0)