Implementing a Custom Low Stock Activity
Here, you’ll learn how to add a custom low stock activity by implementing the CommerceLowStockActivity interface.
Low stock activities are actions happen automatically if products fall below their configured minimum stock quantities. Liferay provides one default low stock activity, which unpublishes the product.
Deploy an Example Low Stock Activity
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 Low Stock Activity.
-
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 Docker container console.
-
Verify that the example low stock activity was added. Open your browser to
https://localhost:8080
. Open the Global 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”) is present under the Low Stock Action drop-down.
In Liferay Commerce 2.1 and earlier, find the products page by navigating to Control Panel → Commerce → Products.
Anatomy of a Low Stock Activity
This example has three steps:
- Annotate the class for OSGi Registration
- Review the
CommerceLowStockActivity
interface - Complete the Low Stock Activity
Annotate the Class for OSGi Registration
It’s important to provide a distinct key for the low stock activity so that Liferay 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 this activity appears in the UI. For example, the Set as Unpublished activity has a value of 10. Giving this low stock activity a value of 9 ensures that it appears immediately before the Set as Unpublished" activity.
Review the CommerceLowStockActivity
Interface
Implement the following methods:
This method is where you implement the business logic for the custom activity.
This provides a unique identifier for the low stock activity in the low stock activity registry. You can use the key to fetch the low stock activity from the registry.
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 comprises of backend logic to perform the activity itself.
Add Business Logic to execute
This adds a warning message to Liferay’s logs.
The cpInstance
object contains information about the item with low stock. This example uses it to get the SKU for the item to add to the warning message. See CPInstance and CPInstanceModel to find more methods you can use with a CPInstance
.
Add the Language Key to Language.properties
Add the language key and its value to a Language.properties file within the module:
See Localizing Your Application for more information.
Congratulations! You now know the basics for adding a new low stock activity to Liferay by implementing the CommerceLowStockActivity
interface.