HomeDev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

Warehouses and inventories examples

Describes how to use the Optimizely Commerce (PaaS) Framework API with warehouses and inventory features.

Inventories

Get an instance of IInventoryService

Use IInventoryService.GetCacheSkippingInstance(IEnumerable) 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(); 
      }

Get 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(); 
      }

List 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) 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, int, int, out int) to list all inventories by entry code in a specified range.

// List all inventory records by entry code in a specified range.
    public IEnumerable<InventoryRecord> QueryInventoriesInRangeByEntry(IEnumerable<string> catalogEntryCodes, int offset, int count, out int totalCount)
      {
        var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
        return inventoryService.QueryByEntry(catalogEntryCodes, offset, count, out totalCount);
      }

Use IInventoryService.QueryByWarehouse(IEnumerable) 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, 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) 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, 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);
    }

Request 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);
      }

Update inventories

Use IInventoryService.Save(IEnumerable) 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) 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) 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) 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) 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) 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) 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);
      }​

Warehouses

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

List 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();
    }

Get 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);
      }

Save 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 (PaaS) 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);
      }

Delete a warehouse

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

public void DeleteWarehouse(int warehouseId)
      {
        var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
        warehouseRepository.Delete(warehouseId);
      }