JavaScript API
Mini cart#
| Member | Description |
|---|---|
window.nbwtMiniCart.open() | Opens the drawer. |
window.nbwtMiniCart.close() | Closes it. |
window.nbwtMiniCart.refresh() | Fetches the drawer and every fragment again. Returns a Promise. Call it after your plugin changes the cart without WooCommerce’s add-to-cart. |
Any element with the trigger class (default .nbwt-cart-open, set on the Mini cart tab) opens the drawer.
Events#
Plain DOM CustomEvents on document.body. No jQuery needed.
| Event | When |
|---|---|
nbwt_mini_cart_opened | The drawer opened. |
nbwt_mini_cart_closed | The drawer closed. |
nbwt_mini_cart_refreshed | New drawer HTML is in the page. Re-bind anything inside it here. |
document.body.addEventListener( 'nbwt_mini_cart_refreshed', function () {
// Re-init your widgets inside .nbwt-mc
} ); The drawer also listens for WC Currency Switch’s wccs:currency event and fetches itself again in the new currency.
Endpoints#
The drawer posts to these WooCommerce AJAX endpoints. Each returns WooCommerce’s refreshed fragments.
| Endpoint | Fields |
|---|---|
?wc-ajax=add_to_cart | Add on a recommendation, then fires WooCommerce’s added_to_cart event. |
?wc-ajax=nbwt_mc_coupon | code, do = apply or remove. |
?wc-ajax=nbwt_save_later | key. |
?wc-ajax=nbwt_mc_load | The drawer’s light first load. |
Cart events (tracking)#
With a tracking plugin active, nbwtTrack( type, el, qty, response ) sends add, remove and view events from the mini cart and cart page. Every event also fires nbwt_track on document.body with { type, item, quantity }:
document.body.addEventListener( 'nbwt_track', function ( e ) {
// e.detail.type: 'add', 'remove' or 'view'
// e.detail.item: id, name, price, qty...
} );