Building a Seamless Ecommerce Experience with SvelteKit and Shopify

When it comes to ecommerce solutions, a well-functioning cart is crucial. It’s essential to provide users with a convenient way to add and remove items from their cart. In this article, we’ll explore how to manage the cart using the Shopify Storefront cart API and SvelteKit.

Getting Started

If you’re new to this project, you can check out our deployed site on Netlify and explore the code on GitHub. Take a moment to familiarize yourself with the site, and then let’s dive in.

Adding Items to the Cart

On our product details page, we have an “Add to Cart” button. When clicked, we want to send a request to our add-to-cart endpoint, which will then create a cart or add the product to an existing one. To achieve this, we’ll use Svelte API routes.

In the src/routes folder, create a new folder called api and add a file called add-to-cart.js. This endpoint will receive the product data and make a POST request to Shopify.

The Logic Behind the Scenes

When the “Add to Cart” button is clicked, we collect the necessary details (cartId, itemId, and quantity) and post them to our endpoint. Then, we check if a valid cartId is present in the request body. If it is, we add the product to the cart. Otherwise, we create a new cart with the product data.

Updating the Product Page

Now that we have our endpoint set up, we can define an addToCart function to post data to this endpoint. This function collects the itemId, cartId, and selected quantity, and posts it to our add-to-cart endpoint.

Making it Visible

To make it clear when an item has been added to the cart, we’ll create a Header component to display the cart count. We’ll update our src/routes/__layout.svelte file to import and use the new Header component.

Creating a Cart Page

At this point, we need a cart page to view the items in the cart. Let’s create a cart.svelte page in the src/routes folder. On this page, we’ll get our cart data from localStorage and conditionally show the items in the cart.

Removing Items from the Cart

To remove items from the cart, we’ll create a /remove-from-cart endpoint and update the cart in localStorage with the new response. We’ll then hook up this endpoint to our Remove Item button in the CartTable component.

The Final Result

And that’s it! We’ve successfully set up our cart functionality using SvelteKit and the Shopify Storefront cart API. If you have any issues or want to contribute to the project, feel free to open an issue or send a PR on the project repository. The demo is also hosted on Netlify if you’d like to explore the site.

Optimize Your Ecommerce Flow with LogRocket

LogRocket is a powerful tool that helps you identify and fix issues in your ecommerce flow. By recording everything that happens on your app, LogRocket surfaces the root cause of issues that are preventing conversion in your funnel. Start proactively monitoring your ecommerce apps today!

Leave a Reply

Your email address will not be published. Required fields are marked *