Loading...
Area: Episerver Commerce
Applies to versions: 13 and higher
Other versions:

Warehouses and inventories examples

This topic provides examples of using the Episerver Commerce Framework API with warehouses and inventory features.

Working with inventories

Getting an instance of IInventoryService

Use IInventoryService.GetCacheSkippingInstance(IEnumerable<string>) to get an instance of IInventoryService that skips the cache on reads.

Getting a single inventory

Use IInventoryService.Get(string, string) to get a single inventory record for a catalog entry and a warehouse.

Listing inventories

Use IInventoryService.List() to list information for all inventories.

Use IInventoryService.QueryByEntry(IEnumerable<string>) to list all inventories by entry code.

Use IInventoryService.QueryByEntry(IEnumerable<string>, int, int, out int) to list all inventories by entry code in a specified range.

Use IInventoryService.QueryByWarehouse(IEnumerable<string>) to list all inventories by warehouse code.

Use IInventoryService.QueryByWarehouse(IEnumerable<string>, int, int, out int) to list all inventories by warehouse code in a specified range.

Use IInventoryService.QueryByPartialKey(IEnumerable<InventoryKey>) to list all inventories matching an item in an inventory key.

Use IInventoryService.QueryByPartialKey(IEnumerable<InventoryKey>, int, int, out int) to list all inventories matching an item in an inventory key in a specified range.

Requesting a transactional inventory operation

Use IInventoryService.Request(InventoryRequest request) to request a transactional inventory operation.

Updating inventories

Use IInventoryService.Save(IEnumerable<InventoryRecord>) to save specified inventory records. Records are matched to existing records on InventoryRecord.CatalogEntryCode or  InventoryRecord.WarehouseCode values, and are updated if a match is found, and inserted if no match is found.

Use IInventoryService.Insert(IEnumerable<InventoryRecord>) to insert the specified inventory records. An exception is thrown if any records exist with matching InventoryRecord.CatalogEntryCode and InventoryRecord.WarehouseCode values.

Use IInventoryService.Update(IEnumerable<InventoryRecord>) to update the specified inventory records. Records are matched to existing records on InventoryRecord.CatalogEntryCode and InventoryRecord.WarehouseCode values. And, an exception is thrown for any records that do not match.

Use IInventoryService.Delete(IEnumerable<InventoryKey>) to delete all specified inventory data.

Use IInventoryService.Adjust(IEnumerable<InventoryChange>) to increment or decrement matching values in the inventory provider. Elements of changes are matched to existing data by item and warehouse code, and each quantity in the InventoryService.InventoryChange is added to the corresponding quantity in the inventory provider.

Use IInventoryService.DeleteByEntry(IEnumerable<string>) to delete all inventory data for specified catalog entries.

Use IInventoryService.DeleteByWarehouse(IEnumerable<string>) to delete all inventory data for specified warehouses.

Working with warehouses

The following examples show how to get, save, and delete warehouses.

Listing warehouses

Use IWarehouseRepository.List() to list information for all warehouses.

Getting a warehouse

Use IWarehouseRepository.Get() to get a single warehouse. You can get a warehouse by Id or by Code.

Saving a warehouse

Use IWarehouseRepository.Save(IWarehouse) to add/edit and save a warehouse.

Note: The IWarehouse object returned from Get and List methods above IWarehouseRepository is read-only. So, to edit it, you must make a writable clone object then update it, as is.

Note: If you have Commerce 12 or higher, you can improve the UpdateWarehouse section of the above code like this:

public void UpdateWarehouse(string warehouseCode)
  {
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
    var warehouse = warehouseRepository.Get(warehouseCode); // It's a read-only object
    var writableCloneWarehouse = warehouse.CreateWritableClone();
    writableCloneWarehouse.IsPickupLocation = true;
    warehouseRepository.Service.Save(writableCloneWarehouse);
  }

Deleting a warehouse

Use IWarehouseRepository.Delete(int) to delete a warehouse.

Last updated: Jun 07, 2019