Adding a New Product Data Source for the Product Publisher Widget
This tutorial shows you how to add a new product data source by implementing the CPDataSource
interface.
Product data sources provide unique ways to search for products that are related. Liferay Commerce provides several product data sources out-of-the-box, including ones that search by product relations and by categories.
Overview
Deploy an Example
In this section, get an example product data source up and running on your instance of Liferay Commerce.
Start a new Liferay instance by running
Sign in to Liferay at http://localhost:8080. Use the email address test@liferay.com and the password test. When prompted, change the password to learn.
Then, follow these steps:
-
Download and unzip the Acme Commerce Product Data Source.
-
Build and deploy the example.
NoteThis command is the same as copying the deployed jars to
/opt/liferay/osgi/modules
on the Docker container. -
Confirm the deployment in the Liferay Docker container console.
-
Verify that the example product data source was added. Open your browser to
https://localhost:8080
and navigate to a page with a Product Publisher widget. Click Configuration for the Product Publisher, then select Data Source under the Product Selection section. The new product data source (“Products Ending in the Same Word”) appears under the Data Source drop-down below.
Congratulations, you’ve successfully built and deployed a new product data source that implements CPDataSource
.
Next, let’s dive deeper to learn more.
Walk Through the Example
In this section, review the example we deployed. First, annotate the class for OSGi registration. Second, review the CPDataSource
interface. And third, complete our implementation of CPDataSource
.
Annotate Your Class for OSGi Registration
The product data source name must be a unique value so that Liferay Commerce can distinguish the new data from existing data sources.
Review the CPDataSource
Interface
Implement the following methods:
This method returns a text label that describes how product data source searches for related products. See the M5X7CPDataSource.java
class in liferay-m5x7.zip/m5x7-impl/src/main/java/com/acme/m5x7/internal/commerce/product/data/source
for a reference in retrieving the label with a language key.
This returns the name of the product data source.
This is where we add the business logic to perform the search for related products. The HttpServletRequest
contains a reference to a particular product which the results should be related to in some way.
The method returns a CPDataSourceResult
, which contains a list with the search results; see the implementation at CPDataSourceResult.java.
Complete the Product Data Source
The product data source is comprised of logic to perform a search for related products. Do the following:
Add the Search Logic to getResult
We use a CPDefinitionHelper to perform the search. The CPDefinitionHelper
combines logic specific to product definitions with BaseIndexer
’s search functionality; see BaseIndexer.java for more information.
Add the product definition’s ID as the value for the "excludedCPDefinitionId"
attribute to the SearchContext
. This omits the original product from the results. In our example, we also specify the last word of the product name to search for. For details, see the search logic implementation in the example M5X7CPDataSource.java
file’s _getLastWordOfName
method.
Add the Language Key to Language.properties
Add the language key and its value to a Language.properties
file in liferay-m5x7.zip/m5x7-impl/src/main/resources/content
within our module:
See Localizing Your Application for more information.
Conclusion
Congratulations! You now know the basics for implementing the CPDataSource
interface, and have added a new product data source to Liferay Commerce.