BlogHow-toGoing live

How to connect Vendra to WooCommerce, a headless API or your own backend

What the cart, wishlist and compare do in the browser, where the product data comes from, and the two files to replace when the shop goes live, for WooCommerce, a headless platform or your own API.

4 min readPerforks

The two files to replace when the shop goes live, between the pages in the browser and WooCommerce or an API

Vendra is a front-end. Out of the box the whole shop runs in the browser, so you can show it, click through it and agree on it before a single API exists. Connecting it means replacing two small files; the pages do not change.

What runs in the browser today

PartWhereHow it works
Product datasrc/data/catalog.jsEvery page is generated from this file at build time. The product page, search and quick view also read it at run time from a JSON block in each page, <script id="vdCatalog" type="application/json">.
Cart, wishlist, comparesrc/js/store.jsKept in localStorage under vendra-cart, vendra-wishlist and vendra-compare. A cart line is the product id plus the chosen colour and size, with a quantity from 1 to 99. Every change writes the storage and fires an event (vd:cart, vd:wishlist, vd:compare) that the counters and the drawer listen to.
Totals, promo code, ordersrc/js/pages/checkout.jstotals() adds the demo tax (8%) and shipping (free from $75, otherwise $4.95 standard) and applies the promo codes WELCOME10, FREESHIP and VENDRA20; placing the order stores it in sessionStorage as vendra-order, clears the cart and opens order-complete.html, which reads it back.
Card fieldscheckout.htmlA visual demo only. Nothing is sent anywhere.

The pages talk to these files only through their functions and events, which is what makes the swap contained.

Step 1: decide where the products come from

You have two options, and they can be combined.

  • Keep generating pages from a file. Export your products from WooCommerce, Shopify, Medusa or your database into the shape of catalog.js and rebuild. Simple, fast pages, and the browser cart keeps working unchanged. Good for catalogues that change a few times a day at most.
  • Fetch at run time. Replace getCatalog() in store.js, which today parses the vdCatalog block, with a call to your API, and render cards from the result. In the React edition the catalogue is react/src/catalog.generated.json, injected by react/src/engine.js into the same block, so the same function is the place to change.

A card carries its product as JSON in data-vd-product (id, slug, name, price, compare-at price, image, url, category, rating, colours, sizes, stock, SKU). Whatever renders a card only has to put that attribute on it; the engine does the rest.

Step 2: replace the store

store.js exports three objects with a small surface:

cart.items()  cart.count()  cart.subtotal()
cart.add(product, { color, size }, qty)  cart.setQty(key, qty)  cart.remove(key)  cart.clear()
wishlist.ids()  wishlist.has(id)  wishlist.toggle(id)  wishlist.remove(id)  wishlist.clear()
compare.ids()   compare.has(id)   compare.toggle(id)   compare.remove(id)   compare.clear()

Make each one call your backend instead of localStorage and keep firing the same events (the file's emit() helper), so the header counters, the drawer and the pages update as they do now. For WooCommerce that is the Store API cart endpoints; for a headless platform, its cart or checkout API; for your own backend, whatever you expose. Because the functions are synchronous today, the cleanest approach is to keep a local copy of the cart in memory, update it optimistically, and sync with the server in the background.

Step 3: replace totals and the order

In pages/checkout.js, totals() becomes a call to your backend's quote (tax and shipping are rarely a flat rule), and the submit handler of the checkout form becomes the order creation. Use your payment provider's hosted fields or redirect, so card data never touches your pages; the demo card fields are there to be replaced.

Three common set-ups

BackendWhat you do with Vendra
WooCommerce as a headless shopKeep the pages, read products from the WooCommerce REST or Store API, map the cart to the Store API, hand checkout to WooCommerce.
WooCommerce as a WordPress themeTurn the partials in src/partials/ into PHP templates and let WooCommerce drive data, cart and checkout; keep the styles, the settings and the effects.
Your own APISame two files; the data source changes, the pages stay.

What you do not have to touch

The settings panel, the effects, the search modal, the filters, the quick view, the size guide and every page layout keep working as they are. The filters and the search work on what is on the page and in the catalogue block, so if your catalogue is fetched at run time, feed that block (or the function that reads it) and they follow.

Keep the licence in mind: a shop whose visitors buy products is a Regular end product; a platform that other people pay to build shops on needs the Extended licence. See Regular or Extended.