Implementing a Custom Low Stock Activity
This tutorial will show you how to add a custom low stock activity by implementing the CommerceLowStockActivity interface.
Low stock activities are actions that are automatically taken if products fall below their configured Minimum Stock Quantities. Liferay Commerce provides one default low stock activity, which is to unpublish the product.
Overview
Deploy an Example
In this section, we will get an example low stock activity up and running on your instance of Liferay Commerce.
Start a new Liferay instance by running
docker run -it -m 8g -p 8080:8080 liferay/portal:7.4.3.55-ga55
Sign in to Liferay at http://localhost:8080. Use the email address [email protected] and the password test. When prompted, change the password to learn.
Then, follow these steps:
Download and unzip the Acme Commerce Low Stock Activity.
curl https://resources.learn.liferay.com/commerce/latest/en/developer-guide/managing-inventory/liferay-j1e4.zip -O
unzip liferay-j1e4.zip
Build and deploy the example.
./gradlew deploy -Ddeploy.docker.container.id=$(docker ps -lq)
noteThis command is the same as copying the deployed jars to
/opt/liferay/osgi/modules
on the Docker container.Confirm the deployment in the Docker container console.
STARTED com.acme.j1e4.impl_1.0.0
Verify that the example low stock activity was added. Open your browser to
https://localhost:8080
. Click the Applications Menu () and navigate to Commerce → Products. Then click Edit within the menu for any product. If necessary, you can add a product.
From there, navigate to Configuration. The new activity (“Log a warning message”) will be present under the Low Stock Action dropdown.
In Liferay Commerce 2.1 and earlier, find the products page by navigating to Control Panel → Commerce → Products.
Congratulations, you’ve successfully built and deployed a new low stock activity that implements CommerceLowStockActivity
.
Next, let’s dive deeper to learn more.
Walk Through the Example
In this section, we will review the example we deployed. First, we will annotate the class for OSGi registration. Second, we will review the CommerceLowStockActivity
interface. And third, we will complete our implementation of CommerceLowStockActivity
.
Annotate the Class for OSGi Registration
@Component(
property = {
"commerce.low.stock.activity.key=j1e4",
"commerce.low.stock.activity.priority:Integer=9"
},
service = CommerceLowStockActivity.class
)
public class J1E4CommerceLowStockActivity implements CommerceLowStockActivity {
It is important to provide a distinct key for the low stock activity so that Liferay Commerce can distinguish the new activity from others in the low stock activity registry. Reusing a key that is already in use will override the existing associated activity.
The
commerce.low.stock.activity.priority
value indicates how far into the list of low stock activities our activity will appear in the UI. For example, the “set as unpublished” activity has a value of 10. Giving our low stock activity a value of 9 ensures that it will appear immediately before the “set as unpublished” activity.
Review the CommerceLowStockActivity
Interface
Implement the following methods:
public void execute(CPInstance cpInstance) throws PortalException;
This method will be where the business logic is implemented for the custom activity.
public String getKey();
This provides a unique identifier for the low stock activity in the low stock activity registry. The key can be used to fetch the low stock activity from the registry.
public String getLabel(Locale locale);
This returns a text label that describes the low stock activity. See the implementation in J1E4CommerceLowStockActivity.java for a reference in retrieving the label with a language key.
Complete the Low Stock Activity
The low stock activity is comprised of backend logic to perform the activity itself. Do the following:
Add Business Logic to execute
@Override
public void execute(CPInstance cpInstance) throws PortalException {
if (_log.isWarnEnabled()) {
_log.warn("SKU " + cpInstance.getSku());
}
}
In our example, we add a warning message that is added to Liferay’s logs.
The
cpInstance
object contains information that we can use about the item with low stock. In our example, we use it to get the SKU for the item to add to our warning message. See CPInstance and CPInstanceModel to find more methods you can use with aCPInstance
.
Add the Language Key to Language.properties
Add the language key and its value to a Language.properties file within our module:
j1e4-commerce-low-stock-activity=J1E4 Commerce Low Stock Activity
See Localizing Your Application for more information.
Conclusion
Congratulations! You now know the basics for implementing the CommerceLowStockActivity
interface and have added a new low stock activity to Liferay Commerce.