spartacus-storefront

Customize the QuoteService in QuoteModule


I have the need to customize some action on the quotes page. I already implemented the backend part, so the action has its function in a custom quote helper method. So far this was not a big deal but I seem to be unable to inject the proper service (my custom one) into the QuoteModule.

The quote service is overridden like this:

@Injectable()
export class CustomQuoteService extends QuoteService {
    // custom implementation...
    override performQuoteAction(
        quote: Quote,
        quoteAction: QuoteActionType
    ): Observable<unknown> {
        console.error("performQuoteAction", { quote, quoteAction })
        return this.performQuoteActionCommand.execute({ quote, quoteAction });
}

and in the module we provide it

CustomQuoteService,
{
  provide: QuoteService,
  useExisting: CustomQuoteService,
},

However, in the QuoteModule, which is lazy loaded it still uses the OOTB implementation. The quote-feature module imports like this:

provideConfig(<CmsConfig>{
  featureModules: {
    [QUOTE_FEATURE]: {
      module: () =>
        import('@spartacus/quote').then((m) => m.QuoteModule),
    },
  }
}),

Someone might share some light on me, how to replace the service. Thanks


Solution

  • It turned out, that the provider needs to be put into a separate module that is loaded lazy as well.
    So a module holding the provider

    @NgModule({
      imports: [
        QuoteModule,
      ],
      providers: [
        {
          provide: QuoteService,
          useClass: CustomQuoteService,
        }
      ]
    })
    export class CustomQuoteModule {}
    

    loaded in the via featureModules

    provideConfig({
      featureModules: {
        [QUOTE_FEATURE]: {
          module: () => import('./cust-quote.module').then((m) => m.CustomQuoteModule),
        }
      }
    }),
    

    is solving the issue with components partially using the customized service.