Skip to content

Swap the price resolver

The default resolver selects channel and customer-group price-list rows.

The default resolver selects channel and customer-group price-list rows. Wrap it when you need instrumentation or a small policy change; replace it only when selection semantics truly differ.

Register a host resolver

import type { ApplicationService } from "@adonisjs/core/types";
import { Database } from "@adonisjs/lucid/database";
import type { ResolvedCommerceConfig } from "@adocommercekit/core";
import { DatabasePriceResolver } from "@adocommercekit/core/drivers";
import type { PriceResolver } from "@adocommercekit/core/types";

export class ContractPriceResolver implements PriceResolver {
  constructor(private fallback: DatabasePriceResolver) {}

  async resolve(input: Parameters<PriceResolver["resolve"]>[0]) {
    // Resolve a contract price here. Return the fallback when none exists.
    return this.fallback.resolve(input);
  }
}

export default class CommerceOverridesProvider {
  constructor(private app: ApplicationService) {}

  register() {
    this.app.container.singleton(ContractPriceResolver, async (resolver) => {
      const db = await resolver.make(Database);
      const config = this.app.config.get<ResolvedCommerceConfig>("commerce");
      return new ContractPriceResolver(new DatabasePriceResolver(db, config));
    });
  }
}

Register the host provider before @adocommercekit/core/commerce_provider, then point config at it:

pricing: {
  resolver: async (app) => app.container.make(ContractPriceResolver),
  taxInclusivePricing: true,
}

Preserve resolver invariants

A resolver must return exactly one ResolvedPrice in the requested currency or throw E_PRICE_NOT_FOUND. It must honor quantity, channel, customer context, and an optional transaction client. The result’s source metadata should identify the selected rule for diagnostics.

Cart recalculation calls the resolver for every line. Keep lookups indexed and avoid remote calls on this path. If prices originate remotely, synchronize them into a local table or put a bounded cache behind a deterministic fallback.

Navigation

Type to search…

↑↓ navigate↵ selectEsc close