Skip to content

JavaScript API

JavaScript API

Bilberry Widgets provides a JavaScript API that allows you to programmatically control widget behavior and interact with the basket and cart functionality.

Overview

The JavaScript API consists of two main categories:

  1. Configuration Functions - For updating widget settings, themes, and language (see existing documentation)
  2. Action Functions - For controlling basket visibility and cart operations (documented below)

Configuration Functions

The following configuration functions are already documented in their respective sections:

Action Functions

The BilberryActions object provides functions to control basket visibility and cart operations.

openBasket()

Opens the basket modal/sidebar.

window.BilberryActions.openBasket();

Example:

// Open basket when user clicks a custom button
document.getElementById('my-custom-basket-button').addEventListener('click', () => {
window.BilberryActions.openBasket();
});

closeBasket()

Closes the basket modal programmatically.

window.BilberryActions.closeBasket();

Note: The basket modal already includes:

  • A close button (X) in the top-right corner
  • Click-outside-to-close functionality
  • ESC key to close

Example:

// Close basket programmatically (e.g., when user logs out)
function handleLogout() {
window.BilberryActions.closeBasket();
// ... other logout logic
}
// Close basket when switching to a different section
function switchToProductSection() {
window.BilberryActions.closeBasket();
// ... navigate to product section
}

toggleBasket()

Toggles the basket visibility (opens if closed, closes if open).

window.BilberryActions.toggleBasket();

Example:

// Toggle basket with a custom button
document.getElementById('toggle-basket-btn').addEventListener('click', () => {
window.BilberryActions.toggleBasket();
});

clearCart()

Clears all items from the cart.

window.BilberryActions.clearCart();

Example:

// Clear cart when user logs out
function handleLogout() {
window.BilberryActions.clearCart();
// ... other logout logic
}

getItemCount()

Returns the number of items currently in the cart.

window.BilberryActions.getItemCount();

Returns: number - The total number of items in the cart

Example:

// Display item count in a custom basket button
const count = window.BilberryActions.getItemCount();
document.getElementById('my-basket-button').textContent = `Cart (${count})`;

Browser Compatibility

The JavaScript API is available in all modern browsers that support:

  • ES6 modules
  • Custom Elements (Web Components)
  • window object

Notes

  • All functions are available globally on the window object