header-logo
Getting started with WixBuild your website and business with Wix. Try using our AI tools to create your website, customize templates, or hire professionals to build or enhance your online presence.
Creating a websiteRead how to use the Wix Editor and manage content on your site. Discover how to protect your visitors' privacy and make your site accessible.
Account & billingReview your account settings, manage your subscriptions and plans and understand your invoices.
Using the Wix mobile appsUse our mobile apps to run your business efficiently on the go and set up a dedicated app for your site members to stay in touch.
Connecting a domainFollow our step-by step guides to learn how to purchase, connect or transfer a domain for your site, and find out how to keep your site’s data secure.
Managing your businessGet tools to grow your business and web presence. Find the right tools for your specific needs, set up online payments and keep track of your site's analytics.
Promoting your siteLearn to make your site more visible promoting it on search engines, sending emails and running paid ads. Improve your online presence with practical tips.
Studio, Partners & EnterpriseGet the most out of your Studio or Enterprise account with advanced features that'll help you work more efficiently.
Need more help?Find solutions for site performance and technical issues, learn about issues we’re working to fix or contact us for help.
placeholder-preview-image
Wix LearnImprove your skills with our courses and tutorials.
Wix BlogGet tips for web design, marketing and more.
SEO Learning HubLearn to increase organic site traffic from search engines.
Website developmentBuild a custom site using our full-stack platform.
Hire a ProfessionalGet matched with a specialist to help you reach your goals.
placeholder-preview-image

Wix Blocks: Widget API Functions

Wix Blocks is currently open to a limited number of users.
Add a new function to enable site creators to perform actions related to the widgets on their websites. For example, a site creator who has a shopping widget would like to be able to get the address of a certain customer through the function getAddress(customerId) and set the address through the function setAddress(customerId).
To add a new public function to your widget:

1.  Click the Widget API  icon.
2.  Click Add New Public Function or hover over Functions and click the  icon. 
add new public function
3.  Add the code for your function in the code panel, where a new function "foo" appears. 
4.  Change the new default name to a meaningful name and edit the function.
1import {getJSON} from 'wix-fetch';
2
3/**
4*@function
5*@description Add item to cart
6*@param {string} productId - The Id of the product to add to the cart
7*@param {number} quantity - How many products to add
8*@returns {Promise<boolean>} True on success, false on error
9*/
10export async function addItemToCart(productId, quantity){
11 try {
12  const addToCartReponse = await getJSON('https://myCustomStoreService/cart', {
13   method: 'POST',
14   body: JSON.stringify({
15    productId,
16    quantity,
17   })
18  })
19
20  return addToCartReponse.success
21 } catch (e) {
22  return false
23 }
24}
Don't forget to document
When you write your function, make sure to replace the automatic jsdoc that appears above it with your own one. The user of the widget will see the jsdoc when using the function in a site. 

Next Step: Using the Widget API on a Site

Want to learn more about Blocks?
Check out the Wix Blocks help articles

Helpmate

Hello

Need a bit more guidance?
Unlock personalized helpLog in to get the most out of Helpmate.