Try our conversational search powered by Generative AI!

Petter Sørby
Apr 10, 2015
  6093
(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
Product Listing Page - using Graph

Optimizely Graph makes it possible to query your data in an advanced way, by using GraphQL. Querying data, using facets and search phrases, is very...

Jonas Bergqvist | Jul 5, 2024

Optimizely Search and Navigation - Part 2 - Filter Tips

Introduction Continuing from Part 1 – Search Tips , today I will share the next part – filter tips. The platform versions used for this article are...

Binh Nguyen Thi | Jul 1, 2024

Integrating HubSpot CRM without the MA Connector

Have HubSpot CRM? Want to push user data into it from Optimizely? Don’t have any personalisation requirements with that data? Don’t want to pay $80...

Matt Pallatt | Jun 27, 2024

Keeping the website secure by updating external packages

Did you see the latest warning from Optimizely to update this package with a critical security warning? https://world.optimizely.com/documentation/...

Daniel Ovaska | Jun 27, 2024

Optimizely CMS image anonymization now available for Linux!

The famous image anonymization add-on for Optimizely CMS, with at least 5 downloads, is now finally available for use on Linux. Supports simultaneo...

Tomas Hensrud Gulla | Jun 25, 2024 | Syndicated blog

Remove a segment from the URL in CMS 12

Problem : I have created thousands of pages dynamically using schedule jobs with different templates (e.g. one column, two columns, etc..) and stor...

Sanjay Kumar | Jun 21, 2024