BlogReferenceEngine

What Vendra does in the browser: cart, wishlist, compare, quick view, filters, search, and the JavaScript API

How each browser-side feature is implemented, the data attributes that bind them, the limits built into the demo, the window.Vendra API and the events the engine fires.

3 min readPerforks

The data attributes that bind the cart, wishlist and compare, the window.Vendra API and the events the engine fires

The engine is src/js/, built into vendra.min.js and shared by the React edition. It binds to markup through data-vd-* attributes, so a block works on any page that has the attribute.

The features

FeatureHow it worksMarkup
CartLines of product id + colour + size with a quantity from 1 to 99, in localStorage. Adding opens the drawer; counters and the drawer re-render on the vd:cart event. Free shipping from $75 drives the drawer's meter.data-vd-add on a button inside a data-vd-product element; data-vd-direct skips the options dialog; data-vd-open-cart; data-vd-count="cart"
WishlistA list of product ids, toggled from any card; the page renders cards for them from the catalogue.data-vd-wish, data-vd-count="wishlist"
CompareThe same, with a maximum of four; a fifth replaces the oldest. The page builds a table with a differences-only switch.data-vd-compare, data-vd-count="compare"
Quick viewA modal filled from the card's JSON: gallery, options, quantity, add to cart, link to the product. Opens on its own when a product with several colours or sizes is added from a card.data-vd-quick, modal #vdQuickView
FiltersClient-side over the cards on the page, using the card's data attributes for category, price, rating, colours, sizes and sale: category, price range, colour, size, rating, on sale, in stock, plus sorting (featured, newest, price up or down, rating, name). The state is written to the URL and read back, so a filtered view can be linked.form data-vd-filters, select data-vd-sort
SearchAccent- and case-insensitive match of every term against name, category, SKU and demo, over the page's catalogue block, with the matches highlighted; results after a short pause while typing.input data-vd-search, modal #vdSearch
CheckoutTotals with the demo tax of 8%, shipping (free from $75, else the chosen option), and promo codes WELCOME10 (10% off), FREESHIP and VENDRA20 (20% off). Placing the order writes it to sessionStorage, clears the cart and opens the confirmation.data-vd-promo, page scripts in src/js/pages/checkout.js
SettingsOne JSON object in localStorage applied as attributes on <html> before the first paint by an inline script, then by the engine.data-vd-setting, data-vd-toggle, data-vd-reset

Where a product's data comes from: the card holds it as JSON in data-vd-product; the page holds the whole catalogue in <script id="vdCatalog" type="application/json">. Pages are keyed to their script by <body data-page="shop|product|cart|checkout|order-complete|wishlist|compare">.

The window.Vendra API

After the page loads, the engine exposes itself as window.Vendra and fires vd:ready.

Vendra.version                                  // '1.0.0'
Vendra.cart.add(product, { color, size }, qty)
Vendra.cart.items() / .count() / .subtotal()
Vendra.cart.setQty(key, qty) / .remove(key) / .clear()
Vendra.wishlist.ids() / .has(id) / .toggle(id) / .remove(id) / .clear()
Vendra.compare.ids()  / .has(id) / .toggle(id) / .remove(id) / .clear()
Vendra.catalog()                                // every product of the page's catalogue block
Vendra.product(slugOrId)
Vendra.search('linen')                          // the matching products
Vendra.quickView(product)
Vendra.toast({ title, text, icon: 'bag' })
Vendra.money(89)                                // '$89.00'
Vendra.setTheme(mode) · Vendra.setDir(dir) · Vendra.setDemo(demo)
Vendra.settings.get() / .set('accent', '#7d1f2c') / .fx('cursor', true) / .fonts(pack)
Vendra.bootstrap                                // the Bootstrap instance, also window.bootstrap

A product is the object a card carries (Vendra.product('linen-overshirt') returns one). In the React edition the same object is window.Vendra too, with edition: 'react'.

Events

All fire on document, with details in event.detail:

  • vd:cart: { action: 'add', product, qty, opts }, { action: 'qty', key, qty } or { action: 'clear' }.
  • vd:wishlist, vd:compare: { id, on }, or { clear: true }.
  • vd:settings: the settings object after a change.
  • vd:ready: the API, once the engine has bound the page.
document.addEventListener('vd:cart', (e) => console.log(e.detail))

Limits and demo values

  • Quantity per line: 1 to 99. Compare: 4 products. Free shipping threshold: $75. Tax: 8%. Standard shipping: $4.95.
  • Prices are formatted in US dollars by money(); the currency switch in the header changes the label only.
  • The card fields on the checkout page are a visual demo and send nothing.

Replacing the storage and the totals with a backend is covered in connect Vendra to WooCommerce, a headless API or your own backend; where the state is kept, and why it can look empty, in the cart is empty after a reload.