Search code examples
spartacus-storefront

How to replace an existing spartacus facade with my own custom implementation?


For example a custom adapter can be easily provided because we have an interface and a default implementation. With implementing the interface and provide the custom implementation in a providers array I can change the implementation on all usages.

Unfortunately for the services in the facade layer are no interfaces, only the implementation.

So the question is how can I provide a custom implementation of a facade (e.g. ActiveCartService) in spartacus and ensure that also usages in the spartacus framework uses my custom implementation?


Solution

  • Supposing you have your custom implementation of ActiveCartService:

    import { ActiveCartService } from '@spartacus/core';
    import { Injectable } from '@angular/core';
    
    @Injectable()
    export class CustomActiveCartService extends ActiveCartService {
      /* .... */
    }
    

    Please provide your implementation of ActiveCartService in some module (i.e. in app.module):

    @NgModule({
      providers: [
        {
          provide: ActiveCartService,
          useClass: CustomActiveCartService,
        }
      ],
      /* ... */
    })
    export class AppModule {}