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
docker run -it -m 8g -p 8080:8080 liferay/portal:7.4.3.112-ga112
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.
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
. 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
@Component(
property = {
"commerce.low.stock.activity.key=j1e4",
"commerce.low.stock.activity.priority:Integer=9"
},
service = CommerceLowStockActivity.class
)
public class J1E4CommerceLowStockActivity implements CommerceLowStockActivity {
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:
public void execute(CPInstance cpInstance) throws PortalException;
This method is where you implement the business logic for the custom activity.
public String getKey();
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.
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 comprises of backend logic to perform the activity itself.
Add Business Logic to execute
@Override
public void execute(CPInstance cpInstance) throws PortalException {
if (_log.isInfoEnabled()) {
_log.info("SKU " + cpInstance.getSku());
}
}
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:
j1e4-commerce-low-stock-activity=J1E4 Commerce Low Stock Activity
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.