Parcourir la source

add get product api

api-template
davey il y a 4 jours
Parent
révision
69bdfb8464
1 fichiers modifiés avec 56 ajouts et 0 suppressions
  1. 56
    0
      API/shopify/storefront/product/getProduct.js

+ 56
- 0
API/shopify/storefront/product/getProduct.js Voir le fichier

@@ -0,0 +1,56 @@
1
+const storefrontClient = require('../storefrontClient');
2
+
3
+const customQuery = 'status:active, product_type:snowboard'; // Replace to see custom result
4
+
5
+const maxResults = 10; // Replace to change the number of result
6
+
7
+const sortBy = 'TITLE'; // Replace with CREATED_AT if need to sort by date, current value is for name 
8
+
9
+const getProduct = async (customQuery) => {
10
+    try {
11
+ 
12
+        const query = `{
13
+            products (first: ${maxResults}, sortKey: ${sortBy}, query: ${customQuery}) {
14
+                edges {
15
+                    node {
16
+                        id
17
+                        title
18
+                        status
19
+                        productType
20
+                        comparisonAtPriceRange {
21
+                            minVariantPrice {
22
+                                amount
23
+                                currencyCode
24
+                            }
25
+                            maxVariantPrice {
26
+                                amount
27
+                                currencyCode
28
+                            }
29
+                        }
30
+                        variants(first: 5) {
31
+                            edges {
32
+                                node {
33
+                                    id
34
+                                    title
35
+                                    price
36
+                                }
37
+                            }
38
+                        }
39
+                    }
40
+                }
41
+                pageInfo {
42
+                        hasNextPage
43
+                        hasPreviousPage
44
+                }
45
+            }
46
+        }`;
47
+
48
+        const products = await storefrontClient.query({ data: query });
49
+
50
+        return products;
51
+    } catch (error) {
52
+        throw error;
53
+    }
54
+};
55
+
56
+getProduct(customQuery); 

Chargement…
Annuler
Enregistrer