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

Warehouses and inventories examples

Recommendations [hide]

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.

// Gets an instance of IInventoryService that will skip the cache on reads. 
public IInventoryService GetCacheSkippingInstance() 
  {  
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>(); 
    return inventoryService.GetCacheSkippingInstance(); 
  }

Getting a single inventory

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

// Gets a single inventory record. 
public InventoryRecord GetAnInventory() 
  { 
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>(); 
    return inventoryService.Get(); 
  }

Listing inventories

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

// List all inventory records. 
public IEnumerable<InventoryRecord> ListAllInventories() 
  { 
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();  
    return inventoryService.List(); 
  } ​

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

// List all inventory records by entry code.
public IEnumerable<InventoryRecord> QueryInventoriesByEntry(IEnumerable<string> catalogEntryCodes)
  {
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.QueryByEntry(catalogEntryCodes);
  }​

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.

// List all inventory records by warehouse codes.
public IEnumerable<InventoryRecord> QueryInventoriesByWarehouse(IEnumerable<string> warehouseCodes)
  {
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.QueryByWarehouse(warehouseCodes);
  }​

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

// List all inventory records by warehouse code in specified range.
public IEnumerable<InventoryRecord> QueryInventoriesInRangeByWarehouse(IEnumerable<string> warehouseCodes, int offset, int count, out int totalCount)
  {
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.QueryByWarehouse(warehouseCodes, offset, count, out totalCount);
  }​

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

// List all inventory records matching an item in inventory key.
public IEnumerable<InventoryRecord> QueryInventoriesByPartialKey(IEnumerable<InventoryKey> partialKeys)
  {
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.QueryByPartialKey(partialKeys);
  }​

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

// List all inventory records matching an item in inventory key in specified range.
public IEnumerable<InventoryRecord> QueryInventoriesInRangeByPartialKey(IEnumerable<InventoryKey> partialKeys, int offset, int count, out int totalCount)
{
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.QueryByPartialKey(partialKeys, offset, count, out totalCount);
}​

Requesting a transactional inventory operation

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

// Requests a transactional inventory operation.
public InventoryResponse RequestInventory(InventoryRequest request)
  {
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.Request(request);
  }​

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.

// Saves the specified inventory records.
public void SaveInventories(IEnumerable<InventoryRecord> records)
  {
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.Save(records);
  }​

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.

// Inserts the specified inventory records.
public void InsertInventories(IEnumerable<InventoryRecord> records)
  {
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.Insert(records);
  }​

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.

// Updates the specified inventory records.
public void UpdateInventories(IEnumerable<InventoryRecord> records)
  {
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.Update(records);
  }

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

// Deletes all specified inventory data.
public void DeleteInventories(IEnumerable<InventoryKey> inventoryKeys)
  {
    if (inventoryKeys == null || inventoryKeys.Contains(null))
      {
        throw new ArgumentNullException(nameof(inventoryKeys));
      }
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.Delete(inventoryKeys);
  } ​

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.

// Incerements or decrements matching values in the inventory provider.
public void AdjustInventory(IEnumerable<InventoryChange> changes)
  {
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.Adjust(changes);
  }​

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

// Deletes all inventory data for the specified catalog entries.
public void DeleteInventoriesByEntry(IEnumerable<string> catalogEntryCodes)
  {
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.DeleteByEntry(catalogEntryCodes);
  } ​

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

// Deletes all inventory data for the specified warehouses.
public void DeleteInventoriesByWarehouse(IEnumerable<string> warehouseCode)
  {
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.DeleteByWarehouse(warehouseCode);
  }​

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.

// Get list Warehouse
public IEnumerable<IWarehouse> ListAllWarehouses()
{
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
    return warehouseRepository.List();
}

Getting a warehouse

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

// Get a specific Warehouse by ID
public IWarehouse GetWarehouse(int warehouseId)
  {
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
    return warehouseRepository.Get(warehouseId);
  }
// Get a specific Warehouse by Code
public IWarehouse GetWarehouse(string warehouseCode)
  {
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
    return warehouseRepository.Get(warehouseCode);
  }

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. To edit it, you must make a writable clone object then update it, as is.

public void CreateNewWarehouse()
  {
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
    var warehouse = new Warehouse
      {
        Code = "NY",
        Name = "New York store",
        IsActive = true,
        IsPrimary = false,
        IsFulfillmentCenter = false,
        IsPickupLocation = true,
        IsDeliveryLocation = true,
        ContactInformation = new WarehouseContactInformation
          {
            FirstName = "First Name",
            LastName = "Last Name",
            Line1 = "Address Line 1",
            Line2 = "Address Line 2",
            City = "City",
            State = "State",
            CountryCode = "Country Code",
            PostalCode = "Postal Code",
            RegionCode = "Region Code",
            DaytimePhoneNumber = "Daytime Phone Number",
            EveningPhoneNumber = "Evening Phone Number",
            FaxNumber = "Fax Number",
            Email = "Email"
          }
      };
    warehouseRepository.Save(warehouse);
  }

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

Note: For 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.

public void DeleteWarehouse(int warehouseId)
  {
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
    warehouseRepository.Delete(warehouseId);
  }
Do you find this information helpful? Please log in to provide feedback.

Last updated: Jun 07, 2019

Recommendations [hide]