• Log in
  • Sign up
  • Contact Us

Get in touch  

Developer Guides • Flows | Carts

Manage abandoned carts with Flows

Last updated: 12 Sep 2019

Quick intro on how carts work

There are three aspects of how Moltin carts operate:

  1. Carts are not automatically deleted when the cart is checked out, they are therefore, re-usable.
  2. Existing cart sessions are automatically deleted after 14 days.
  3. Cart sessions are identified by a given ID. You do not create a cart session before adding an item to it. Instead, you add an item to a cart by specifying the item and a cart ID. This will automatically create the cart associated with that cart ID and add the item.

Based on the above facts, we need to store the fact that:

  1. A cart session has been created.
  2. A cart session has been deleted.
  3. A cart session has been used to checkout.

If we can store that information somewhere, we can build an up-to-date list of cart sessions which have not been checked out or deleted. Helpfully, our Flows feature can be used for this purpose, and we will learn how to do that below.

Prerequisites

  • Moltin account (you'll need Client ID and Client Secret of your store).

Summary of steps required

Create a custom Flow for abandoned carts:

  • Create abandoned carts Flow.
  • Create a cart-id and a date-created Fields.
  • Create an Entry to store each new cart session.

Add logic in case the abandon cart is checked out:

  • Delete the cart.
  • Delete the Flow Entry with that cart-id.

Add logic to update abandoned carts Flow after 14 days:

    • GET all entries on the abandoned-cart flow.
    • Loop over each entry and check the created_at date.
    • If the created_at date is more than 14 days ago, remove the entry.

Step-by-step walkthrough

You can use Moltin dashboard to create and manage Flows, but we will explain how to do it via API calls.

Get your access token

You will need to get a client_credentials access token to follow along making the API requests outlined below.

curl -X POST https://api.moltin.com/oauth/access_token \
     -d "client_id=XXXX" \
     -d "client_secret=XXXX" \
     -d "grant_type=client_credentials"

Create a custom Flow (abandoned carts)

You first create a Flow called "Abandoned Carts". Make sure to note the returned ID of the created Flow as we will need it in the next step! You can find the full reference for this call in our API Reference.

curl -X POST https://api.moltin.com/v2/flows \
     -H "Authorization: XXXX" \
     -H "Content-Type: application/json" \
     -d $'{
        "data": {
          "type": "flow",
          "name": "Abandoned Carts",
          "slug": "abandoned-carts",
          "description": "Entity to hold abandoned cart information",
          "enabled": true
        }
     }'

Create a Flow Field for cart IDs

Let's create a Field to hold our cart IDs for carts that have been abandoned and a field to hold the date the cart session was created. You can find the full reference for the call to add a Flow Field in our API Reference.

curl -X POST https://api.moltin.com/v2/fields \
     -H "Authorization: XXXX" \
     -H "Content-Type: application/json" \
     -d $'{
      "data": {
        "type": "field",
        "name": "Cart ID",
        "slug": "cart-id",
        "field_type": "string",
        "description": "Abandoned cart ID",
        "required": false,
        "unique": false,
        "default": "",
        "enabled": true,
        "omit_null": false,
        "relationships": {
            "flow": {
                "data": {
                    "type": "flow",
                    "id": "FLOW-ID"
                }
            }
        }
      }
    }'

Repeat the above call for the date-created Field. The Field type in this case is date and the name and slug must be changed to reflect the new Field details.

Set up your application

Great! We have a data structure ready to hold cart IDs. Next we need to identify when to add them and remove them from that list and how to do so.

Adding a cart to the list

In order for a cart to be abandoned, it must first be created and used. This is the easiest point at which to add a cart ID to the list. The most basic way to do this is to create a Flow Entry every time a new cart session is created.

You will know this is a new cart if there is no Moltin cart already associated with the user of your application. In this case, you will make the following POST call to v2/flows/abandoned-carts/entries.

curl -X POST https://api.moltin.com/v2/flows/:flow-slug/entries \
     -H "Authorization: XXXX" \
     -H "Content-Type: application/json" \
     -d $'{
       "data": {
         "type": "entry",
         "cart-id": "CART_ID_GOES_HERE",
         "date-created": "DATE_GOES_HERE"
       }
     }'

Removing a cart from the list

After checkout

If the user actually checks out their cart at any time before 14 days after it was first used, we want to make sure that the cart is no longer marked as abandoned. We will also want to make sure that cart is deleted so there is no conflict in cart sessions.

You can add two calls from your application to Moltin once the user has successfully checked out:

      1. Delete the cart (see: API Reference).
      2. Delete the Flow Entry for that cart ID:
curl -X DELETE https://api.moltin.com/v2/flows/:flow-slug/entries/:entry-id \
    -H "Authorization: Bearer XXXX"

In order to make this call, you first need to fetch the Flow Entries, loop through them, and check if the cart ID is present in any of the Entries. If you find it, you will have the Entry ID, which you can then use in the URL of the DELETE call above.

After 14 days

If the user has not checked out after 14 days, the cart session is automatically wiped by Moltin. We want to make sure that our list is kept in line with that fact. Therefore, you should write a short script which runs once a day. The script should do the following:

    1. GET all entries on the abandoned-cart flow.
    2. Loop over each entry and check the date-created.
    3. If the date-created is more than 14 days ago, remove the entry.

Further reading

From Our Blog

14 Jun 2019

Extend the order and cart resource with Flows

Extend the order resource to add shipping provider and a consignment number.

Read More
author
Joanna Suau
in Flows | Orders | Carts
14 Jun 2019

Store data form third party integration with Flows

Store nexus locations from a tax calculation service using Flows.

Read More
author
Joanna Suau
in Flows | Integrations
13 Jun 2019

Create a Blog schema with Flows

Add custom data to create and manage blog.

Read More
author
Jamie Barton
in Flows