If you are serious about learning SAP Hybris Commerce, you must learn the fundamentals of the Groovy scripting language. Groovy is an integral part of Hybris, and it will accelerate your Hybris Commerce development and testing efforts. For example, let's say you know nothing about the Hybris Stock or Warehouse Services. How would you learn more about these Hybris Services? Answer: Use Groovy to expedite your learning of these important services. With this tutorial, I will show you how to retrieve a single product's stock level from a particular warehouse using Groovy.
The BaseCommerce and ElectronicsStore extensions and their essential data are loaded in your Hybris Implementation.
Step by Step Instructions
1. Goto to HAC in your Web browser. (If Hybris is running on your local machine, type https://localhost:9002/ in your Web browser URL)
2. Type your user name and password once HAC is displayed. (The following screenshot displays the HAC login page with username and password provided)
3. Once you've logged in successfully, click on Console -> Scripting Languages
4. Copy and paste the following code into the Console window:
FlexibleSearchService fss = spring.getBean("flexibleSearchService")
CatalogVersionService cvs = spring.getBean("catalogVersionService")
WarehouseService ws = spring.getBean("warehouseService")
StockService ss = spring.getBean("stockService")
CatalogVersionModel cvm = cvs.getCatalogVersion("electronicsProductCatalog","Online")
ProductModel pm = new ProductModel()
pm = fss.getModelByExample(pm)
WarehouseModel warehouseModel = warehouseService.getWarehouseForCode("warehouse_n");
int inventoryForSingleWarehouse = stockService.getStockLevelAmount(pm, warehouseModel)
println "Warehouse Name: " + warehouseModel.getName()
println "Product Name: " + pm.getName()
println "Stock Level Amount: " + inventoryForSingleWarehouse
Your Console should resemble the following:
5. Click on Execute
6. Next, Click on the Output tab, you should see the following.
7. Tutorial is done.
Please spend time familiarizing yourself with this Groovy script in HAC, and in about 45 minutes, you should have a respectable grasp of the Hybris Stock and Warehouse Services.