Weblutions Documentation
Weblutions Main Site Contact Us Our Discord
FaxStore IconFaxStore

Abandoned Carts Routes & Events

By Josh M. 10 mins 1

A reference for the routes, events and functions behind Abandoned Carts, for staff who build apps or integrations. Apps can listen for the events below with faxstore.on(), the same way as the other FaxStore Events.

Table of Contents


How It Works

  1. Saving carts. Every change to a cart goes through faxstore.carts.save(), which saves users.cart and stamps users.cartUpdatedAt. Guests' carts go through faxstore.carts.saveGuest() into the guestcarts table, kept by the fxcart cookie.

  2. The sweep. Every 10 minutes a sweep runs, and does five things:

    • Abandoned carts: carts left for longer than Count a cart as abandoned after become rows in the abandonedcarts table, and cartAbandoned fires. Guests count only once they've given their email and ticked the reminders box.

    • Lost carts: open carts left for longer than Mark a cart lost after become lost.

    • Reminders: the next reminder that's due is sent for each open cart, by email, Discord DM or both, unless it's quiet hours for the customer. cartReminderSent fires.

    • Browse reminders: these go out to customers who viewed a product often enough (recorded in productviews by faxstore.carts.viewed()), and browseReminderSent fires.

    • Cleanup: once a day, expired recovery coupons that were never used, and guest carts unused for 60 days, are deleted.

  3. Checkouts. When a checkout finishes, faxstore.carts.converted() marks the customer's cart recovered or purchased, and cartRecovered fires. This covers PayPal, Stripe, Square, free checkouts and Tebex.

  4. Survey answers. When a customer answers the survey (from an email or the exit offer), cartFeedback fires.

  5. Signing in. When a guest signs in to a verified account, getUserInfo() calls faxstore.carts.mergeGuest(), which adds their guest cart to the account's cart.

A cart's last activity is the later of its last change and the last time the customer started checkout (from checkout_sessions).


Events

Listen with faxstore.on(name, function(...) {}). Each event passes the cart object first, then an object with more detail. cartAbandoned doesn't fire for older carts (carts from before this update). For guests, its second argument includes guest: true. For carts started by the exit offer, it includes source: 'exit-offer'.

Name

Arguments

Description

Type

cartAbandoned

cart, {items}

A cart has been left for longer than Count a cart as abandoned after. items lists each product: {id, title, option, price}.

on

cartReminderSent

cart, {step, automatic, by, coupon, subject, variant, channel}

A reminder was sent. step is 1 to 3, or null for a custom email. automatic is false when staff sent it, and by is that staff member's user ID. coupon is the code included, or null. variant is A or B during an A/B test, otherwise null. channel is email, discord or discord+email.

on

cartRecovered

cart, {status, via, paymentId, total}

The customer checked out. status is recovered (a reminder, the exit offer or the on-site reminder brought them back) or purchased (they came back on their own). via is email-click, exit-offer, nudge, email or direct. paymentId is null for free checkouts and tebex for Tebex.

on

cartFeedback

cart, {reason, label, comment}

The customer answered the survey. reason is the reason's key: price, browsing, payment, info, elsewhere or other for the starting reasons, or r and six hexadecimal characters for reasons the store added. label is the reason as shown to the customer. comment is their comment, or null. Answers from the exit offer also have source: 'exit-offer'.

on

browseReminderSent

{userId, productId, views}, {channel, subject}

A browse reminder was sent for a product the customer viewed views times.

on

The existing checkout events still fire as before: createCheckout, checkoutReturn, checkoutCancel and checkoutExpired.

Example. An app that posts recovered carts to a Discord channel:

module.exports = function(app, connection, bot, faxstore) {
    faxstore.on('cartRecovered', function(cart, details) {
        if(details.status !== 'recovered') return;
        const channel = faxstore.discord.bot.channels.cache.get('1093456789012345678');
        channel?.send(`A customer came back and spent $${details.total} (${details.via}).`).catch(() => {});
    });
};

The Cart Object

The first argument to every event is the cart's row from the abandonedcarts table. The link token and timeline are left out.

Field

Description

id

The cart's ID.

userId

The customer's user ID, or an empty string for a guest.

guestCartId, guestEmail

For guests: their guestcarts row and email.

exitOfferAt

When the customer used the exit offer.

cart

The cart's contents, in the same format as users.cart: comma-separated entries like 12, 12pack:3, 12tebx:4567 or 12note:Large.

items

The number of items.

value

The cart's value at list price.

stage

cart, or checkout if the customer started checking out.

status

open, recovered, purchased, emptied, lost or dismissed.

legacy

1 for a cart from before this update.

abandonedAt

When the cart was found to be abandoned (milliseconds).

lastActivityAt

The customer's last activity in the cart. 0 for older carts.

step

The last reminder sent (0 to 3).

emailsSent

Reminder emails sent.

lastEmailAt

When the last reminder was sent.

clicks, clickedAt

Clicks on the email's button, and the first click.

nudgedAt

When the customer opened their cart from the on-site reminder.

paused, stopReason

Whether automatic reminders are stopped, and why: staff, feedback or unsubscribed.

couponId, couponCode, couponAt

The recovery coupon's promocodes ID, its code, and when it was made.

feedback, feedbackComment, feedbackAt

The customer's survey answer: the reason's key, their comment and when they answered. The label they saw is kept in the timeline.

paymentId, recoveredValue, recoveredVia, closedAt

How the cart was closed.

createdAt

When the row was created.


The faxstore.carts Functions

Function

Description

save(userId, cart, {touch, tz})


Saves a cart, given as an array of entries or a comma-separated string, and returns the saved entries. It also sets users.cartUpdatedAt. Pass {touch: false} for changes the store makes itself, like removing a deleted product, so the customer's last activity stays the same. tz is the browser timezone (the fxtz cookie), saved for quiet hours. Saving an empty cart closes the customer's open abandoned cart as emptied. Returns a promise.

saveGuest(req, res, cart, {touch})

The same for a signed-out visitor. The first save creates their guestcarts row and sets the fxcart cookie.

guestInfo(req, siteRow)

For getUserInfo(): the visitor's guest cart, as {cart, info: {email, consent}}, or null.

mergeGuest(req, res, userRow)

Adds a guest cart to a signed-in account's cart, moves its open abandoned cart to the account, and clears the cookie. Returns the merged cart, or null.

guestsEnabled(siteRow)

Whether guest carts are on.

converted(userId, paymentId, total, cart)

Call after a checkout finishes. Marks the customer's latest open or lost cart from the last 30 days as recovered or purchased, and fires cartRecovered. cart (the entries bought) also marks browse reminders for those products as bought. Returns the updated row, or null.

viewed(userId, product)

Records a signed-in customer viewing a product page, for browse reminders. Each customer and product is written at most once every 30 minutes.

exitOfferFor(req, userData, siteRow, cartValue)

What the cart page's exit offer shows: `{mode: 'coupon'

summary(since, periodLabel), sendSummary(to, since, periodLabel)

Builds or emails the abandoned carts summary.

dashboard()

{recovered, recoveredValue, open} for the staff home tile.

nudged(userId)

Records that the customer opened their cart from the on-site reminder.

describe(cart)

Returns {items, total} for a cart. Each item has {entry, id, title, option, price, image, url, itemType, stock, available}. Prices follow the cart page: a package's price, otherwise the sale price, otherwise the price.

sweep(now)

Runs the sweep straight away. Returns false if a sweep is already running.

settings(json)

Reads the settings JSON, filling in defaults.

defaults()

The default settings.

reasons(json)

The survey reasons in a settings JSON, as {key: label}, for example {price: 'Too expensive', ...}.


Settings

Settings are saved as JSON in sitesettings.abandonedCarts. Missing or out-of-range values fall back to these defaults:

{
  "enabled": false,
  "abandonAfterMinutes": 60,
  "minValue": 0,
  "lostAfterDays": 14,
  "steps": [
    {"enabled": true, "delayHours": 1, "coupon": false, "channel": "email", "subject": "Did you forget something?", "subjectB": "", "message": "..."},
    {"enabled": true, "delayHours": 24, "coupon": false, "channel": "email", "subject": "Your cart is still waiting", "subjectB": "", "message": "..."},
    {"enabled": false, "delayHours": 72, "coupon": true, "channel": "email", "subject": "Here's {{DISCOUNT}} off your cart", "subjectB": "", "message": "..."}
  ],
  "coupon": {"enabled": false, "percent": 10, "expiresDays": 3, "cooldownDays": 30, "prefix": "COMEBACK"},
  "feedback": {
    "enabled": true,
    "question": "What stopped you from checking out?",
    "prompt": "Not ready to buy? Tell us why:",
    "comment": true,
    "thanks": "Your answer helps us make the store better.",
    "reasons": [
      {"key": "price", "label": "Too expensive", "enabled": true, "email": true},
      {"key": "browsing", "label": "Just browsing", "enabled": true, "email": true},
      {"key": "payment", "label": "Payment problem", "enabled": true, "email": true},
      {"key": "info", "label": "Needed more info", "enabled": true, "email": true},
      {"key": "elsewhere", "label": "Bought elsewhere", "enabled": true, "email": false},
      {"key": "other", "label": "Something else", "enabled": true, "email": true}
    ]
  },
  "nudge": {"enabled": true, "afterMinutes": 60},
  "alerts": {"enabled": false, "minValue": 50, "discordChannel": "", "recovered": true},
  "guests": {"enabled": true, "capture": true},
  "exitOffer": {"enabled": false, "offer": "auto", "minValue": 0, "mobileDelay": 30, "title": "Wait, before you go!", "message": "..."},
  "browse": {"enabled": false, "minViews": 2, "afterHours": 24, "withinDays": 14, "cooldownDays": 7, "channel": "email", "subject": "Still thinking about {{PRODUCT_NAME}}?", "message": "..."},
  "extras": {"ratings": true, "stock": true, "suggestions": 3},
  "quietHours": {"enabled": false, "start": 21, "end": 8}
}

Field

Range

abandonAfterMinutes

15 to 1,440

lostAfterDays

1 to 90

steps[].delayHours

1 to 720. Each turned-on reminder must be later than the one before.

steps[].channel, browse.channel

email, discord or both

steps[].subjectB

Up to 150 characters. Empty means no A/B test.

exitOffer.offer

auto (a coupon, or the survey if the customer can't have one), coupon or survey

exitOffer.mobileDelay

5 to 300 seconds

browse.minViews

1 to 20

browse.afterHours, browse.withinDays, browse.cooldownDays

1 to 168 hours, 1 to 60 days, and 0 to 90 days

extras.suggestions

0 to 4

quietHours.start, quietHours.end

0 to 23 (hours). The window can wrap past midnight.

coupon.percent

1 to 90

coupon.expiresDays

1 to 30

coupon.cooldownDays

0 to 365

coupon.prefix

Up to 12 letters and numbers

nudge.afterMinutes

5 to 10,080

feedback.question, feedback.prompt

Up to 120 characters

feedback.thanks

Up to 500 characters

feedback.reasons

Up to 10. Each label is up to 40 characters. A reason without a valid key (lowercase letters and numbers, up to 30) is given a new one. An empty list goes back to the starting reasons. At least one must be turned on while the survey is on. Settings saved before the survey could be changed ("feedback": true) still work.


Email Template

Reminders use files/email-templates/cart.html, which is based on the generic template. FaxStore sends this template to abandoned cart customers and guests. To edit the template and change any pre-fills see the Email System & Notifications guide.