Blog • Carts

When to use a custom cart

Last updated: 05 Jul 2019

Custom carts allow you to process an order for any type of item you wish to sell. The item does not need to exists in Moltin, which means custom cart items can be used for all sort of use cases.

Places in the user flow you would use Custom cart:

We will walk you through the three common ways custom carts are leveraged. There is a playground site set up that you can use experiment with your own specific use cases.

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"

Scenario 1. Allow a user to buy a product that's not in Moltin

Setup: A made-up product that does not exist in Moltin. This means it will not have a product_id.

Scenario: A user is purchasing an item that was custom made on your site.

Cart Item example

Field Name Type Description
name string Product name
sku string The unique SKU of the product.
description string Product description
quantity integer Product quantity. Should default to 1
price.amount integer Product price

Moltin object

{
	type: "custom_item",
	name: "Black t-shirt with dragon",
	sku: "123029123",
	description: "This is a custom made t-shirt",
	quantity: 1,
	price: {
		amount: 10000
	}
}

Custom cart example

 const item = {
    //Type will alyways be custom_item
    type: "custom_item",
    name: productName.value,
    sku: productSku.value,
    description: productDesc.value,
    quantity: productQty.value,
    price: {
      amount: productAmount.value
    }
  }

    Moltin.Cart(customCartId)
      .AddCustomItem(item)
      .then(cart => {
        console.log(JSON.stringify(cart))
    })

Scenario 2 Adding shipping fees to a product

Setup: A user may want to buy a product from a different country which requires an additional shipping fee. In this example, we will apply a flat fee. This is meant to give you a basic example of how this could work with your own internal shipping logic or a third party shipping API.

Scenario: A user based in Newcastle, UK wishes to buy a shirt from Boston, US. This is going to cost more than what was originally accounted for in shipping fees.

Cart Item example

Field Name Type Description
name string Product name
sku string The unique SKU of the product.
description string Product description
quantity integer Product quantity. Should default to 1
price.amount integer Product price

Moltin object

{
	type: "custom_item",
	name: "Black t-shirt with dragon",
	sku: "123029123",
	description: "This is a custom made t-shirt",
	quantity: 1,
	price: {
		amount: 10000
	}
}

Custom cart example

 const shippingCountry = "UK"
let item
if (shippingCountry != "US") {
	item = {
		type: "custom_item",
		name: "Black t-shirt with dragon",
		sku: "123029123",
		description: "This is a custom made t-shirt",
		quantity: 1,
		price: {
			amount: 10000 + 500
		}
	}
}
Moltin.Cart(exampleCart2).AddCustomItem(item).then(cart => {
	console.log(JSON.stringify(cart))
})

Scenario 3 Adding tax to a cart

Setup: You may want to collect a set amount of tax after you find out where the user is shipping the product to. You are able to add a tax amount as an item to the user's order to account for the tax you need to collect. This is a basic example of how this could work with your own internal tax logic or a third party API.

Scenario: A user based in Boston wants to buy a bottle of wine from your California vineyard. You need to collect 7.25% tax on the order.

Cart Item example

Field Name Type Description
name string Product name
sku string The unique SKU of the product.
description string Product description
quantity integer Product quantity. Should default to 1
price.amount integer Product price

Moltin object

{
	type: "custom_item",
	name: "CA TAX",
	sku: "9238091",
	description: "TAX from CA to ship out of state.",
	quantity: 1,
	price: {
		amount: 210000 * .0725
	}
}

Custom cart example

 const shippingCountry = "NY"
let item
if (shippingState != "CA") {
	item = {
		//Type will alyways be custom_item
		type: "custom_item",
		name: "CA TAX",
		sku: "9238091",
		description: "TAX from CA to ship out of state.",
		quantity: 1,
		price: {
			amount: 210000 * .0725
		}
	}
}
Moltin.Cart(exampleCart3)
	.AddCustomItem(item)
	.then(cart => {
		document.example3Frame.document.body.innerHTML = JSON.stringify(cart)
		console.log(JSON.stringify(cart))
	})
}

Summary

The cart service flexibility is a powerful service, and allows you to enable all sorts of commerce. The use cases are endless for the custom cart, and can be used to solve any roadblocks you might have when interacting with customers.

Where to go from here

These basic examples are meant to give you an idea of how you could use a custom cart. A logical next step would be to integrate your own or third party logic with the custom cart. An example of third party API to get tax rates or arrange shipping would be TaxJar or Shippo.

Further reading