Blog • Flows | Products

Create a wishlist with Flows

Last updated: 05 Jul 2019

Prerequisites

Moltin dashboard (you'll need Client ID and Client Secret of your store). Good understanding of object-oriented programming or JavaScript.

Summary of steps required:

  • Create a Flow that will contain your wishlist object.
  • Create products Field to store customer's desired products.
  • Create empty Entries to associate with products.
  • Create a relationship between an Entry and a product. This will be created every time a customer adds a product to a wishlist.
  • Associate a wishlist with a customer by adding a customers Flow and creating a one-to-many relationship between a customer and a wishlist.

1. 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"

2. Create a new custom Flow

Using the Moltin API, go ahead and create a flow for "Wishlist".

curl -X POST "https://api.moltin.com/v2/flows" \
     -H "Authorization: XXXX" \
     -H "Content-Type: application/json" \
     -d $'{
      "data": {
        "type": "flow",
        "name": "Wishlist",
        "slug": "wishlist",
        "description": "Allow customers to store products they want to purchase at a later date",
        "enabled": true
      }
    }'

Make sure to take note of the Flow ID returned, you'll need this below as WISHLIST-FLOW-ID.

3. Tell a Wishlist it will have Products (create a Flow Field)

In this step we will create a field for `products`. This field will store the customers desired products.

curl -X POST "https://api.moltin.com/v2/fields" \
     -H "Authorization: XXXX" \
     -H "Content-Type: application/json" \
     -d $'{
      "data": {
        "type": "field",
        "name": "Products",
        "slug": "products",
        "field_type": "relationship",
        "validation_rules": [{
          "type": "one-to-many",
          "to": "product"
        }],
        "description": "Wishlist Products",
        "unique": false,
        "required": false,
        "enabled": true,
        "relationships": {
          "flow": {
            "data": {
              "type": "flow",
              "id": "WISHLIST-FLOW-ID"
            }
          }
        }
      }
    }'

4. Create an empty Wishlist (Flow Entry)

With our custom Flow configured, we next create an empty Entry so we can associate products.

curl -X POST "https://api.moltin.com/v2/flows/wishlist/entries" \
     -H "Authorization: XXXX" \
     -H "Content-Type: application/json" \
     -d $'{
      "data": {
        "type": "entry"
      }
    }'

5. Add Product to Wishlist

With our custom Flow entry, we can now associate products with a wishlist entry. You'll need ENTRY-ID and PRODUCT-FIELD-SLUG from above to complete this request.

curl -X POST https://api.moltin.com/v2/flows/wishlist/entries/:entry-id/relationships/:product-field-slug \
     -H "Authorization: XXXX" \
     -H "Content-Type: application/json" \
     -d $'{
      "data": [
        {
            "type": "product",
            "id": "ba9ba29d-06da-4ba9-9e2e-f0e776703324"
        }
      ]
    }'

6. Get all Wishlists

curl https://api.moltin.com/v2/flows/wishlist \
     -H "Authorization: Bearer XXXX"

The response will contain the associated products **per entry** like below.

{
  "data": {
    "id": "ENTRY-ID",
    "type": "entry",
    "relationships": {
      "products": {
        "data": [
          {
            "type": "product",
            "id": "ba9ba29d-06da-4ba9-9e2e-f0e776703324"
          }]
      }
    }
  }
}

Due to the implicit grant type, it is important we connect wishlist and customers. We'll do that next by extending the customer resource.

7. Extend the Customers Entity (create a Customer Flow)

We now need to associate a wishlist to a customer.

If you already have a Flow for customers, skip this step.

curl -X POST https://api.moltin.com/v2/flows \
     -H "Authorization: XXXX" \
     -H "Content-Type: application/json" \
     -d $'{
      "data": {
        "type": "flow",
        "name": "Customers",
        "slug": "customers",
        "description": "Extends the default customer object",
        "enabled": true
      }
    }'

8. Link a customer to a wishlist (configure relationship)

Now let’s create the relationships field that will link a customer to a wishlist (note that we’re going to create a 'one to many' relationship because we might want customers to create multiple wishlists):

curl -X POST https://api.moltin.com/v2/fields \
     -H "Authorization: XXXX" \
     -H "Content-Type: application/json" \
     -d $'{
      "data": {
        "type": "field",
        "name": "Wishlists",
        "slug": "wishlists",
        "field_type": "relationship",
        "validation_rules": [{
          "type": "one-to-many",
          "to": "product"
        }],
        "description": "Customers wishlists",
        "unique": false,
        "enabled": true,
        "relationships": {
          "flow": {
            "data": {
              "type": "flow",
              "id": “CUSTOMER-FLOW-ID"
            }
          }
        }
      }
    }'

We now have the ability to send updates to customers, including their relationships:

curl -X POST https://api.moltin.com/v2/customers/:customer-id/relationships/wishlists \
    -H "Authorization: Bearer XXXX" \
    -d $'{
     "data": [{
        "type": "wishlist",
        "id": "WISHLIST-ENTRY-ID}"
      }]
    }'

9. Get a customers wishlists

We’ve saved the wishlist to a customer, so when we make API calls to get that customer we will now see the wishlist(s) directly on the customer data object. Don't forget to set ?include=wishlists to get the wishlist objects.

curl -X GET https://api.moltin.com/v2/customers/:customer-id?include=wishlists \
     -H "Authorization: Bearer XXXX"

The response will look like this:

{
  "data": {
    "id": "c8c1c511-beef-4812-9b7a-9f92c587217c",
    "type": "customer",
    "name": "Ron Swanson",
    "email": "ron@swanson.com",
    "password": true,
    "relationships": [
      ("wishlists": [
        {
          "type": "entry",
          "id": "WISHLIST-ENTRY-ID"
        }
      ])
    ],
    "included": {
      "wishlists": [
        {
          "id": "WISHLIST-ENTRY-ID",
          "type": "entry",
          "relationships": {
            "products": {
              "data": [
                {
                  "type": "product",
                  "id": "ba9ba29d-06da-4ba9-9e2e-f0e776703324"
                },
                {
                  "type": "product",
                  "id": "394916e8-1d47-44a0-b5d0-a5a61b71bab8"
                }
              ]
            }
          }
        }
      ]
    }
  }
}

Adding a new wishlist

To summarise the steps above, in order to add a new wishlist with new products and a customer who owns it, you should do the following:

  1. Create a new empty entry in the wishlist flow (Step 4)
  2. Add products to the new entry (Step 5)
  3. Associate the entry with a customer (Step 8)

Deleting a wishlist

In order to delete the wishlist, you just need its entry ID, which you can get by following step 9. Once you have the ID, you can call the following:

curl -X DELETE https://api.moltin.com/v2/flows/wishlist/entries/:entry-id \
     -H "Authorization: XXXX"

After you have done this, you should also delete the relationship which exists on the customer, by following the delete method outlined here.

Updating a wishlist

Should your customer wish to add another product to their wishlist, you can follow step 5, which as per the docs, will append a new relationship without affecting the existing related products.

Should your customer wish to remove a product from their wishlist, you should follow the delete method, again outlined here.

Further reading