# Storefront SDK

This is list of exposed events and actions for 3rd party apps. It provides the ability to interact with shop data, functions. For example, retrieving products, collections, carts, etc.

{% hint style="info" %}
ShopBase Storefront leverage latest technology to provide a fast shopping experience for buyers, we uses single page app stack (VueJS, VueX) on ShopBase Storefront so make sure you listen to page change events and update your apps accordingly.
{% endhint %}

### Ready

**Specify a function to execute when the SDK is fully loaded.**

```javascript
window.sbsdk.ready(function() {
    console.log('ready');
});
```

### Get content

**Get page context**

```javascript
window.sbsdk.page.getContext() // { type: 'product', payload: { id: 1 }}
```

**Get current product**

```
window.sbsdk.product.current() // { id: 1000000003239458, title: "T-shirt" }
```

**Get cart**

```javascript
window.sbsdk.cart.get() // { token: '', items: [], subtotal_price: 0, total_price: 0 }
```

**Get user**

```javascript
window.sbsdk.user.get() // { id: 1 }
```

**Get order**

```javascript
window.sbsdk.checkout.getOrder() // { id: 1, items: [{ product_id: 1, product_title: 'T-shirt', variant_id: 1, variant_title: 'Black/M' }], email: 'john.doe@gmail.com' }
```

### Events <a href="#markdown-header-events" id="markdown-header-events"></a>

**Listen page load**

This event will be fired when the current page is starting to load

```javascript
window.sbsdk.page.onLoad(function() {
  console.log('Page loaded')
})
```

**Listen page context update**

```javascript
window.sbsdk.page.onContextUpdate(function(context) {
  console.log(context)
})
```

**Listen add to cart**

```javascript
window.sbsdk.cart.onAdd(function(payload) {
  console.log('Add item', payload) // { variant_id: 1, qty: 1 }
})
```

**Listen cart update**

```javascript
// Update an item
window.sbsdk.cart.onAdd(function(payload) {
  console.log('Update item', payload) // { variant_id: 1, qty: 1 }
})

// Update multiple items
window.sbsdk.cart.onUpdate(function(payload) {
  console.log('Update items', payload) // [ { variant_id: 1, qty: 1 }, { variant_id: 2, qty: 5 } ]
})
```

**Listen remove cart item**

```javascript
window.sbsdk.cart.onRemove(function(payload) {
  console.log('Remove item', payload) // { variant_id: 1 }
})
```

**Listen an user authorized**

```javascript
window.sbsdk.user.onAuthorized(function(user) {
  console.log('User', user) // { id: 1 }
})
```

**Listen an user unauthorized**

```javascript
window.sbsdk.user.onUnauthorized(function() {
  // Do something
})
```

**Listen order completed**

This event is fired when the current order has been completed

```javascript
window.sbsdk.checkout.onCompleteOrder(function(payload) {
	// do something
})
```

#### Actions <a href="#markdown-header-actions" id="markdown-header-actions"></a>

**Add to cart**

Add a product with variant id `123456` into cart.

```javascript
window.sbsdk.cart.add(123456, 1).then(function(response) {
  console.log('Added', response) // { success: true }
}).catch(function(e) {
  console.log('Error', e)
})
```

**Update cart item**

Update quantity of an existing product with variant id `123456` in cart.

```javascript
window.sbsdk.cart.update(123456, 2).then(function(response) {
  console.log('Updated', response) // { success: true }
}).catch(function(e) {
  console.log('Error', e)
})
```

**Update cart items**

```javascript
window.sbsdk.cart.updateMultiple({ 123456: 1, 123457: 2}).then(function(response) {
  console.log('Updated', response) // { success: true }
}).catch(function(e) {
  console.log('Error', e)
})
```

**Remove cart item**

```javascript
window.sbsdk.cart.remove(123456).then(function(response) {
  console.log('Removed', response) // { success: true }
}).catch(function(e) {
  console.log('Error', e)
})
```

**Clear all items in cart**

```javascript
window.sbsdk.cart.clear().then(function(response) {
  console.log('Cleared', response) // { success: true }
}).catch(function(e) {
  console.log('Error', e)
})
```

**Add a discount code**

```javascript
window.sbsdk.checkout.addDiscount('10OFF').then(function(response) {
  console.log('Added', response) // { success: true }
}).catch(function(e) {
  console.log('Error', e)
})
```

**Remove a discount code**

```javascript
window.sbsdk.checkout.removeDiscount('10OFF').then(function(response) {
  console.log('Removed', response) // { success: true }
}).catch(function(e) {
  console.log('Error', e)
})
```

#### Navigate to sign in <a href="#markdown-header-navigate-to-sign-in" id="markdown-header-navigate-to-sign-in"></a>

```javascript
window.sbsdk.user.navigateSignIn()
```

#### Navigate to sign up <a href="#markdown-header-navigate-to-sign-up" id="markdown-header-navigate-to-sign-up"></a>

```javascript
window.sbsdk.user.navigateSignUp()
```

#### Navigate to checkout <a href="#markdown-header-navigate-to-checkout" id="markdown-header-navigate-to-checkout"></a>

```javascript
window.sbsdk.checkout.navigateCheckout()
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.shopbase.com/build-a-theme/storefront-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
