Try our conversational search powered by Generative AI!

Petter Sørby
Apr 10, 2015
  6070
(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
From Procrastination to Proficiency: Navigating Your Journey to Web Experimentation Certification

Hey there, Optimizely enthusiasts!   Join me in celebrating a milestone – I'm officially a certified web experimentation expert! It's an exhilarati...

Silvio Pacitto | May 17, 2024

GPT-4o Now Available for Optimizely via the AI-Assistant plugin!

I am excited to announce that GPT-4o is now available for Optimizely users through the Epicweb AI-Assistant integration. This means you can leverag...

Luc Gosso (MVP) | May 17, 2024 | Syndicated blog

The downside of being too fast

Today when I was tracking down some changes, I came across this commit comment Who wrote this? Me, almost 5 years ago. I did have a chuckle in my...

Quan Mai | May 17, 2024 | Syndicated blog

Optimizely Forms: Safeguarding Your Data

With the rise of cyber threats and privacy concerns, safeguarding sensitive information has become a top priority for businesses across all...

K Khan | May 16, 2024