Skip to main content

Create Custom Components

Learning Objectives

After completing this unit, you’ll be able to:

  • List the ways to add components to a Salesforce Commerce store app.
  • Explain how prebuilt custom Salesforce Commerce Components work.
  • Explain how to use Salesforce Commerce APIs for custom components.
  • List the steps to create a custom component.

Salesforce Commerce Components

The Salesforce Commerce app comes with standard components that are part of the store template. Commerce merchandisers, like Taylor Givens, use them to set up customer experiences. Wei Leung, Ursa Major Solar developer, wants to build custom components, such as headers, footers, and banners for the store app to give Taylor a lot more flexibility.

Here are some of the ways you can add components.

Tool Description

Prebuilt custom D2C components

Use prebuilt display components to customize your store. The components, optimized for mobile, fetch JSON payloads for store pages via embedded API calls and are ready for custom theming in Experience Builder.

Custom D2C LWCs 

Custom LWCs are easy to build and perform well in the web browser that hosts your store.

B2B2C Commerce APIs for custom components

Use Salesforce Commerce component API methods for custom branding and theming.

Use prebuilt custom D2C components, custom D2C LWCs, or B2B2C Commerce APIs for custom components to add new components to your store app.

Wei uses Visual Studio (VS) Code to create a custom LWC component and an SFDX project in which to store it. She also uses VS Code to authorize the org to which SFDX deploys her custom objects. This streamlines deployment.

Custom Lightning Web Components

Lightning web components (LWC) are skeletal files that provide a quick start for creating a variety of custom components for Experience Builder. Access them to get a quick start on creating your components. Custom LWCs consist of these file types.

  • HTML provides the structure for the component.
  • JavaScript defines the core business logic, event handling, API calls to fetch page data, and related metadata.
  • CSS provides the look, feel, and animation.

Prebuilt Custom Components

Wei can download prebuilt display components and deploy them to her store. The components, optimized for mobile, fetch JSON payloads for store pages via embedded API calls and are ready for custom theming in Experience Builder.

Here are some of the components.

  • customCommerceFooter: A footer for custom-themed content that uses the SessionContext interface to update cached data.
  • customCommerceHeader: A header container for placing a banner, logo, navigation, search, and cart badge. The component uses the SessionContext interface to update cached data.
  • cartBadge: Use this component to extend store theming to the cart summary badge, which uses a wire adapter API to display the number of items in the cart.
  • horizontalNavigation, verticalNavigation: After deployment to a store, you can drag these menu container components into a header, footer, or another component.
  • navigationMenu: A hamburger menu container for mobile presentation.
  • linksList: A container to add links to related products, and so on.
  • searchBox: A search box container.
  • storeLogo: A container to receive and display a logo.

Custom Component APIs

Wei can use APIs for data-driven custom theming such as from user IDs or cart summary data. Salesforce Commerce includes a Commerce namespace that supports these component API methods for fetching and displaying data. Here are some of them.

  • getAppContext(): A component that retrieves the application context that doesn’t rely on the current user session, such as the web store ID, or other application-related parameters.
  • getSessionContext(): A component that retrieves session-related context, such as user ID and mode.
  • CartSummaryAdapter: A wire adapter that retrieves the current cart summary.

A component can also use any Experience Cloud APIs, including:

  • getCommunityNavigationMenu(): A component that retrieves menus for display in banner headers and footers.
  • cmsDeliveryApi: A wire adapter that retrieves published CMS content versions for an Experience Builder site.

Create a Custom Component

Here’s how to build a custom component.

  1. Create an HTML file and a JavaScript file with the same name in a shared folder, also with the same name.
  2. Deploy the component to Experience Builder with Sales Commerce-specific metadata. This metadata includes critical statements to ensure your component runs smoothly and safely.
  3. In Experience Builder, combine the components to create custom theming and shopper-friendly features. For example, banners, store logos, navigation menus, search boxes, and linked lists for related products.

The Salesforce Lightning web runtime resolves, compiles, and bundles these files and constructs the component automatically.

Here’s Wei’s checklist to create and deploy a custom component.

Step Task

1

Create an SFDX project.

2

Authorize the org for testing.

3

Create an LWC.

4

Deploy the custom component.

5

Add the component to your D2C Commerce store.

Create an SFDX Project

Wei creates a Salesforce Developer Experience (SFDX) project to store her custom component files. Here’s how she does it.

  1. In the VS Code editor, open the Command Palette by pressing Ctrl+Shift+P (Windows) or Cmd+Shift+P (macOS).
  2. Enter SFDX.
  3. Select SFDX: Create Project.
    In VS Code, create a new project.
  4. Click Enter.
  5. Select Standard.
  6. Name the project ursamajorsolar-project-02 and click Enter.
  7. Select or create a folder to store the project.
  8. Click Create Project.

Authorize the Org for Testing

Wei streamlines deployment by authorizing the org to which SFDX deploys custom objects. Here’s how she does it.

  1. In the VS Code editor, open the Command Palette.
  2. Enter SFDX.
  3. Select SFDX: Authorize an Org.
    SFDX opens the Salesforce login portal to your org in a separate browser window.
  4. Log in using your admin credentials.
  5. If you're prompted to allow access, click Allow. 

After you authenticate in the browser, the CLI remembers your credentials.

Create an LWC

Wei creates a sample custom component to deploy from SFDX to an authorized org. She creates a Lightning web component (LWC) file structure where she can copy and paste content from a component in a GitHub repository. Here’s how she does it.

  1. In the VS Code editor, open the Command Palette.
  2. Enter SFDX.
  3. Select SFDX: Create Lightning Web Component.
    Don’t select SFDX: Create Lightning Component, which creates an Aura component.
  4. Name the component: ursamajorsolar-component-01
  5. To accept the default force-app/main/default/lwc location, press Enter.
  6. To view the new files in VS Code Explorer, press Enter.
  7. Open the HTML file (ursamajor-component-01.html) then copy and paste the code from the corresponding GitHub template HTML file that corresponds to your project.
  8. Save the file.
  9. Open the JavaScript file (ursamajor-component-01.js) then copy and paste the code from the corresponding GitHub template JavaScript file that corresponds to your project.
  10. Save the file.
  11. Open the XML file (ursamajor-component-01.js-meta.xml) then copy and paste the code from the corresponding GitHub template JavaScript file that corresponds to your project.

Deploy a Custom Component to Your Org

Here’s how to deploy the LWC you just created to use it on your store pages

  1. Open the VS Code editor.
  2. From the component project directory, right-click the default folder under force-app/main, and select SFDX: Deploy Source to Org.
  3. On the Output tab of the integrated terminal, view the results of your deployment.
    SFDX displays a deployment status notice that includes an exit code, such as “SFDX: Deploy Source to Org ... ended with exit code 0”.
    Exit code 0 means that the command ran successfully.

Add a Component to Your D2C Store

When you add a component to your canvas in Experience Builder, you can edit its properties to customize it.

  1. In the VS Code editor, open the Command Palette.
  2. Enter SFDX.
  3. Select SFDX: Open Default Org.
    Your store opens in a separate browser.
  4. In the left Navigation panel, click Components.
    The custom component displays under Components | Custom Components.
  5. From the Custom area of the Lightning Components list, drag your LWC to the page canvas.
  6. To edit the component properties, select the component on the page canvas, and enter changes in the floating component property editor.
Note

Before placing a custom component in Experience Builder, make sure you assign a custom theme layout to the page.

Next Steps

In this unit, you learned how to customize a Salesforce Commerce store by creating a custom LWC. Next, let’s explore the shadow DOM.

Resources

Keep learning for
free!
Sign up for an account to continue.
What’s in it for you?
  • Get personalized recommendations for your career goals
  • Practice your skills with hands-on challenges and quizzes
  • Track and share your progress with employers
  • Connect to mentorship and career opportunities