Browse all guides
← All guides

Custom code on a product page: a live sales progress bar

Add your own HTML, CSS and JavaScript to a product page and read that product's live stock, price and rating — for a sales progress bar, a numbered slot picker or an 'only N left' message.

If you can write a little HTML and JavaScript, a product page can show things Tokoo doesn't have a block for. Your code runs in a safe frame inside the product's details, and it can read that product's stock, price and rating, and keeps up to date while the page is open. A typical use is a sales progress bar for a limited batch, or a grid of numbered slots where each variant is one slot with a stock of one.

  1. 1

    Open the product's details

    In Products, open the product (or add a new one) and unfold Product details. Add custom code sits beside Add text and Add image. On the Free plan the button shows a lock — custom code needs Starter.

  2. 2

    Start from an example

    Under Add custom code, pick Sales progress bar or the Only N left message. Either one adds a working section that you can change. Each variant of the product becomes one numbered slot; a slot counts as sold when its stock reaches zero.

  3. 3

    Read the product with Tokoo.onData

    Write your JavaScript inside Tokoo.onData(function (d) { … }). It runs once when the product's data is ready and again each time the stock changes, so redraw everything inside it. Use textContent rather than innerHTML when you show text, so a product name can never be read as code.

  4. 4

    Choose the height

    Grow to fit makes the frame as tall as what your code draws. Choose Fixed if your layout moves around and you'd rather set the height yourself.

  5. 5

    Save, then open the product page

    Save the product, then open it in your store. Stock numbers come from the same figure the Add to cart button uses. Change a number in Locations, or place a test order, and the page updates within about 20 seconds, with no reload.

What d contains inside Tokoo.onData(function (d) { … })

d.store.currency
MYR
d.store.lang
en or zh, the language the shopper is reading
d.product.name
the product's name
d.product.trackInventory
true when the product tracks stock
d.product.rating
{ average, count }, or null when reviews are off
d.product.variants[i].id
the variant's id
d.product.variants[i].label
for example No. 7, or null when the product has no options
d.product.variants[i].priceCents
what the shopper pays now, in cents
d.product.variants[i].regularPriceCents
the price before a sale, or null when nothing is on sale
d.product.variants[i].stock
units that can be bought now, or null when stock isn't tracked
d.product.variants[i].inStock
true or false

Live preview: below the code boxes you'll see your code running as you type, on this product's saved stock and prices — nothing in your store changes until you save. A product you haven't saved yet shows made-up slots instead. Unsaved edits to the variants aren't in the preview until you save them.

On your home page or a custom page: add a Custom code block, then under Products your code can read pick up to 20 products. Your code gets them as d.products — a list in the order you picked, each shaped like d.product above. A block on a store page has no d.product, and a block with no products picked gets no Tokoo at all. The editor canvas shows the same live preview.

The smallest working example: how many slots are left

HTML:
<p id="left"></p>

JavaScript:
Tokoo.onData(function (d) {
  var free = d.product.variants.filter(function (v) {
    return v.inStock;
  }).length;
  document.getElementById("left").textContent =
    free + " of " + d.product.variants.length + " left";
});

Sold so far, when each variant started with the same stock

Tokoo.onData(function (d) {
  var START_STOCK = 1; // what each variant had before the first sale
  var sold = 0;
  d.product.variants.forEach(function (v) {
    sold += START_STOCK - (v.stock || 0);
  });
  document.getElementById("sold").textContent = sold;
});

The page only reads stock; it never writes any. The number is what a shopper could buy right now — units sitting in someone's unpaid order are already taken off, which is why a slot can show as taken a few minutes before the payment clears, and come back if the order lapses.

Your code runs in a separate frame. It can't see your customers, their carts or your orders, and it can't change the page around it. It can't read how many were sold — only what's left — so work out sold from the stock you started with, as in the second example. Tokoo only passes real numbers; please don't use it to show fake countdowns or fake stock.

If you move down to the Free plan, code that is already saved stays in your product but isn't shown, and you'll need to remove it before you can save that product again. Upgrading brings it back.

Still stuck?

Contact us and we'll help you sort it out.

Contact us