Try our conversational search powered by Generative AI!

Petter Sørby
Apr 10, 2015
  6062
(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
Why C# Developers Should Embrace Node.js

Explore why C# developers should embrace Node.js especially with Optimizely's SaaS CMS on the horizon. Understand the shift towards agile web...

Andy Blyth | May 2, 2024 | Syndicated blog

Is Optimizely CMS PaaS the Preferred Choice?

As always, it depends. With it's comprehensive and proven support for complex business needs across various deployment scenarios, it fits very well...

Andy Blyth | May 2, 2024 | Syndicated blog

Adding market segment for Customized Commerce 14

Since v.14 of commerce, the old solution  for adding market segment to the url is not working anymore due to techinal changes of .NET Core. There i...

Oskar Zetterberg | May 2, 2024

Blazor components in Optimizely CMS admin/edit interface

Lab: Integrating Blazor Components into Various Aspects of Optimizely CMS admin/edit interface

Ove Lartelius | May 2, 2024 | Syndicated blog