This tutorial walks you through creating a Wix app that displays a featured product widget using the @wix/stores
SDK module. Here is an image of how the widget looks on a site:
Before getting started, make sure that:
First, let's set up a development site, create the app and add the app to the site.
To create a development site:
To create your app:
Open your terminal.
Run:
When asked what you would like to do, select Create a new Wix App.
When asked how you would like to create your new app, select Create a basic app.
Enter a name for your app.
Enter a project folder name.
Wait as the CLI creates the files for your project.
Navigate to the project folder:
Run the generate command:
This will display a list of extensions that you can add to your app.
Select Site Widget.
When asked which framework to use, select Blocks.
When asked if you'd like to install dependencies, type Y.
After the CLI finishes installing dependencies, install the the Wix Stores package:
Note: You can ignore the warnings in your terminal.
Run the following command to start up a local development server:
When asked to select a Development Site, select Pick an existing site.
Select the site you just created.
Press Enter to install your app on the site.
Click Agree & Add in the browser that opened.
Go back to your terminal and type B to open the Blocks editor.
Now you are in the Blocks Editor, where you'll design your widget.
Click Create Extension > Widget.
Search for the Featured Item widget.
Select the widget and click Add Widget.
Delete the first widget, Widget1, by clicking the three dots next to its name in the Widgets menu.
Click Sync and then Sync Changes to confirm. This syncs your app's UI to your local code files.
In the CLI, type E to open the site in the Editor of your Development Site. Once the site opens, your app is added to the site.
Click + Add Elements > App Widgets.
Click on the Featured Item widget to add it to the canvas.
The widget is now located on the page and uses the site theme to match look and feel of the site.
In this step, you’ll define the widget logic, which presents data according to the product ID.
api.ts
Define a property in the widget API, to hold the product ID.
src
folder of your project.site/widgets/blocks/featured-item/api.ts
load
.myPublicFunction()
.props
array from name
to productId
.Your code should look like this:
widget.ts
The widget.ts
in the same folder is where you add your widget's business logic. You'll use the property you defined in the api.ts
to present the data of the selected product.
Import the SDK stores
module:
Define a helper function updateProduct
inside the defineWidget()
callback that fetches product data using the widget's productId
property and updates the title, subtitle, and image elements accordingly.
Note: The ?
and !
are used because the stores API can also return undefined
.
Replace the boilerplate code of the onReady()
and onPropsChange()
functions. Paste the following code instead, to call the updateProduct()
function. Make sure that all these functions are within the defineWidget()
function.
Delete the boilerplate function, myPublicFunction
, from the return statement.
Save the file.
The Featured Item widget that you added to the app comes with a default settings panel. Let's modify this panel.
Go back to your open Blocks editor.
Click the Editor Experience icon.
Click Custom Panels. You can see that the Featured Item widget has one custom panel, named Settings.
Delete the toggle and text elements.
Click + Add Elements and add a dropdown element.
Click the Settings icon and change the Field title to Choose product (or any other title you want).
Click Sync to update your UI to your code files.
panel.ts
Let's implement the Settings panel logic. The panel gets the list of products and maps them to the dropdown. When the dropdown selection changes, the panel sets the property in the widget API.
When the panel opens, it presents the product that was set in the widget API property. If no property was set, the panel presents the first product in the list.
Go to: site/widgets/blocks/featured-item/panels/settings/panel.ts
Add the following import:
Replace the onReady()
function with this code, which gets the Stores product and sets it to the widget’s productId
property.
}); ``` 4. Save the file.
Back in the Blocks editor, let's add a text element to present the product price. To do this, you'll need to add a paragraph to the widget's stack.
Your finished widget should look like this:
Note: The design changes are not necessary for the app to function.
Let's rename the new element, so that you can refer to it in your code.
#text1
element.price
.updateProduct()
functionLet's update the updateProduct()
function in the widget.ts
.
Add this line under the other lines in the function.
Save the file.
It's time to test the widget in the editor and see the product information update when the panel changes.
Let's add the logic for the button to lead the user to the product page.
Press Ctrl + C
in the terminal, to stop the development server.
Run:
Note: You can ignore the warnings.
In the widget.ts
file, add:
In the updateProduct()
function, add:
Save the file.
You'll be able to test the button on a live site after releasing a version of the app.
Now that you're done developing your app, you can create a version.
Run:
Run dev again:
Click S to open the site.
Go to the widget.
Click the button — it should navigate to the product page.
Congratulations, you’ve created and connected a fully functional, data-powered featured product widget using Blocks and the CLI!