Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Petter Sørby
Apr 10, 2015
  6052
(3 votes)

Faking inventory while waiting for integrations to finalize

In many Commerce projects there will be dependent activities running in parallel. You might be waiting for a warehouse inventory service to be developed while the shopping experience is developed. To prevent a delay in progress on the site without manually adding inventory in Commerce Manager, you can fake the implementation of IWarehouseInventoryService. 

Implement a fake stock in the Catalog

Before

Image BeforeStockFix.png

Fake implementation

    public class FakeWarehouseInventory : IWarehouseInventoryService
    {

        private IWarehouseInventory GetFakeInventory(CatalogKey catalogKey)
        {
            return GetFakeInventory(catalogKey, new Warehouse() {Code = "Default"});
        }

        private IWarehouseInventory GetFakeInventory(CatalogKey catalogKey, IWarehouse warehouse)
        {
           return new WarehouseInventory()
            {
                AllowBackorder = true,
                AllowPreorder = true,
                BackorderAvailabilityDate = DateTime.Now.AddDays(-1),
                BackorderQuantity = 0,
                CatalogKey = catalogKey,
                InStockQuantity = 100,
                InventoryStatus = InventoryTrackingStatus.Enabled,
                PreorderAvailabilityDate = DateTime.Now.AddDays(5),
                PreorderQuantity = 0,
                ReorderMinQuantity = 0,
                ReservedQuantity = 0,
                WarehouseCode = warehouse.Code
            };
            
        }

        public IWarehouseInventory Get(CatalogKey catalogKey, IWarehouse warehouse)
        {
            return GetFakeInventory(catalogKey, warehouse);
        }
        
        public IEnumerable<IWarehouseInventory> List(IEnumerable<CatalogKey> catalogKeys)
        {
            return catalogKeys.Select(GetFakeInventory) ;
        }

        public IEnumerable<IWarehouseInventory> ListTotals(IEnumerable<CatalogKey> catalogKeys)
        {
            return catalogKeys.Select(GetFakeInventory);
        }

 

Use an initializable module to override the default WarehouseInventoryService

[ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
    public class InitializationModule : IConfigurableModule
    {        
        public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context)
        {
            CatalogRouteHelper.MapDefaultHierarchialRouter(RouteTable.Routes, false);            
        }

        public void ConfigureContainer(ServiceConfigurationContext context)
        {            
             context.Container.Configure(ce => ce.For<IWarehouseInventoryService>().Use<FakeWarehouseInventory>());
        }

        public void Preload(string[] parameters)
        {
        }

        public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context)
        {
        }       
    }

After

Image AfterStockFix.png

Download FakeWarehouseInventory.cs

Of course, this approach will not work when you are testing features like tracking stock during purchases.

Creating a purchase order

In order to create a purchase order and make the workflows perform correctly with the fake data, you need to create your own fake implementation of IInventoryService.

Download a FakeInventoryService.cs.

More about warehouse here: http://world.episerver.com/documentation/Items/Developers-Guide/EPiServer-Commerce/75/Warehouses-and-inventories/Multi-warehouse-implementations/

Apr 10, 2015

Comments

Maris Krivtezs
Maris Krivtezs Apr 13, 2015 09:51 PM

Really good solution. You also can add some switch in appSettings and read it at configuration time to be able to switch between real service and fake one.

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog