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.

Low stock activity available out-of-the-box.

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.120-ga120

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:

  1. 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
    
  2. Build and deploy the example.

    ./gradlew deploy -Ddeploy.docker.container.id=$(docker ps -lq)
    
    Note

    This command is the same as copying the deployed jars to /opt/liferay/osgi/modules on the Docker container.

  3. Confirm the deployment in the Docker container console.

    STARTED com.acme.j1e4.impl_1.0.0
    
  4. Verify that the example low stock activity was added. Open your browser to https://localhost:8080. Open the Global Menu (Global Menu) and navigate to CommerceProducts. 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.

Note

In Liferay Commerce 2.1 and earlier, find the products page by navigating to Control PanelCommerceProducts.

The newly added low stock activity

Anatomy of a Low Stock Activity

This example has three steps:

  1. Annotate the class for OSGi Registration
  2. Review the CommerceLowStockActivity interface
  3. 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.

Capabilities

Product

Contact Us

Connect

Powered by Liferay
© 2024 Liferay Inc. All Rights Reserved • Privacy Policy