How Can Learning Groovy Improve Your Hybris Commerce Knowledge?

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.

Prerequisites
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:

import de.hybris.platform.servicelayer.search.FlexibleSearchService
import de.hybris.platform.catalog.CatalogVersionService
import de.hybris.platform.catalog.model.CatalogVersionModel
import de.hybris.platform.core.model.product.ProductModel
import de.hybris.platform.stock.StockService;
import de.hybris.platform.ordersplitting.WarehouseService;
import de.hybris.platform.ordersplitting.model.WarehouseModel;

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.setCode("1099413")
pm.setCatalogVersion(cvm)

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.

Good Luck!

Marc Raygoza

Marc is the Founder of HybrisArchitect.com.
He enjoys helping others learn more about SAP Commerce Cloud (Hybris). Marc is a SAP Commerce Certified Professional and has held the role of SAP Commerce Cloud Architect at Deloitte, PwC, Brillio (a Bain Company), and Nasty Gal. Marc holds an M.S. Software Engineering from Carnegie Mellon University and a B.S. in Accountancy from California State University, Fresno. He can be reached at: mraygoza@hybrisarchitect.com

You may also like...

Popular Posts