diff --git a/netlify.toml b/netlify.toml index 610b16b..45dfa82 100644 --- a/netlify.toml +++ b/netlify.toml @@ -11,3 +11,8 @@ to = "/.netlify/functions/:splat" status = 200 +[[redirects]] + from = "/cart" + to = "/.netlify/functions/cart-view" + status = 200 + diff --git a/netlify/functions/add-to-cart.js b/netlify/functions/add-to-cart.js new file mode 100644 index 0000000..ad4d8a7 --- /dev/null +++ b/netlify/functions/add-to-cart.js @@ -0,0 +1,88 @@ +const { postToShopify } = require('../../src/utils/postToShopify'); +const querystring = require('querystring'); + + +exports.handler = async (event) => { + + // Parse the form submission + if (event.httpMethod !== "POST") { + return { statusCode: 405, body: "Method Not Allowed" }; + } + const { + quantity, + merchandiseId + } = querystring.parse(event.body); + + console.log( quantity, merchandiseId); + + + const response = await postToShopify({ + query: `mutation { createCart($cartInput: CartInput}) { + cartCreate(input: $cartInput) { + cart { + id + createdAt + updatedAt + lines(first:10) { + edges { + node { + id + merchandise { + ... on ProductVariant { + id + } + } + } + } + } + attributes { + key + value + } + estimatedCost { + totalAmount { + amount + currencyCode + } + } + } + } + }`, + variables: `{ + "cartInput": { + "lines" : [ + { + "quantity": "${quantity}", + "merchandiseId": "${merchandiseId}" + } + ], + "attributes": { + "key": "cart_attribute", + "value": "Some cart attribute" + } + } + }` + }); + + console.log(response); + + + // Shopify will create a new cart ID if required + const cardId = "DUMMY" + + // Now that an item was added to the cart, take the user to it. + // We'll render this dynamically from a serverless function + return { + body: "", + statusCode: 302, + headers: { + Location: `/cart?cartId=${cardId}`, + } + }; + + +}; + + + + diff --git a/netlify/functions/cart-view.js b/netlify/functions/cart-view.js new file mode 100644 index 0000000..30fe4b4 --- /dev/null +++ b/netlify/functions/cart-view.js @@ -0,0 +1,13 @@ + +exports.handler = async (event) => { + + console.log(event.queryStringParameters); + + const cartId = event.queryStringParameters.cartId; + + return { + statusCode: 200, + body: `view of cart with ID: ${cartId}` + }; + +} \ No newline at end of file diff --git a/src/site/_includes/product_detail.njk b/src/site/_includes/product_detail.njk index 5782ca6..2e1d341 100644 --- a/src/site/_includes/product_detail.njk +++ b/src/site/_includes/product_detail.njk @@ -3,15 +3,13 @@

{{ item.node.description }}

{{ item.node.images.edges[0].node.alt }} - -
- {% endfor %} - - +
+ diff --git a/src/utils/postToShopify.js b/src/utils/postToShopify.js index dab3a77..176066c 100644 --- a/src/utils/postToShopify.js +++ b/src/utils/postToShopify.js @@ -11,7 +11,12 @@ exports.postToShopify = async ({ query, variables }) => { 'Content-Type': 'application/json', }, body: JSON.stringify({ query, variables }), - }).then((res) => res.json()); + }).then(function(res) { + + console.log(res); + + return res.json(); + }) if (!result || !result.data) { return false;