Skip to main content

How to Override Cart Completion Strategy

In this document, you’ll learn how to override the cart completion strategy.

This guide only explains how to override the cart completion strategy. It’s highly recommended to first understand how Medusa implements the cart completion strategy as explained here.


Step 1: Create Strategy Class

Create a TypeScript or JavaScript file in src/strategiesCopy to Clipboard of your Medusa backend project with a class that extends the AbstractCartCompletionStrategyCopy to Clipboard class:

src/strategies/cart-completion.ts
import { 
AbstractCartCompletionStrategy,
CartCompletionResponse,
IdempotencyKey } from "@medusajs/medusa"
import {
RequestContext,
} from "@medusajs/medusa/dist/types/request"

class CartCompletionStrategy
extends AbstractCartCompletionStrategy {

complete(
cartId: string,
idempotencyKey: IdempotencyKey,
context: RequestContext
): Promise<CartCompletionResponse> {
throw new Error("Method not implemented.")
}

}

export default CartCompletionStrategy
Report Incorrect CodeCopy to Clipboard

The class includes the completeCopy to Clipboard method defined as abstract in AbstractCartCompletionStrategyCopy to Clipboard. At the moment, the method only throws an error.

Using a Constructor

You can use a constructor to access services and resources registered in the dependency container using dependency injection. For example:

src/strategies/cart-completion.ts
// ...
import { IdempotencyKeyService } from "@medusajs/medusa"

type InjectedDependencies = {
idempotencyKeyService: IdempotencyKeyService
}

class CartCompletionStrategy
extends AbstractCartCompletionStrategy {

protected readonly idempotencyKeyService_:
IdempotencyKeyService

constructor(
{ idempotencyKeyService }: InjectedDependencies
) {
super(arguments[0])
this.idempotencyKeyService_ = idempotencyKeyService
}

// ...
}

export default CartCompletionStrategy
Report Incorrect CodeCopy to Clipboard

In the above example, you inject the IdempotencyKeyServiceCopy to Clipboard in the constructor. This allows you to use the IdempotencyKeyServiceCopy to Clipboard within your class.


Step 2: Implement the complete Method

The cart completion strategy is required to implement a single method: the completeCopy to Clipboard method. This method is used in the Complete Cart endpoint to handle the logic of completing the cart.

The method accepts three parameters:

  • cartIdCopy to Clipboard: the first parameter of the method, which is a string indicating the ID of the cart to complete.
  • idempotencyKeyCopy to Clipboard: the second parameter of the method, which is an instance of the IdempotencyKeyCopy to Clipboard entity. The idempotency key is retrieved based on the idempotency key passed in the header of the request, and it’s used to determine the current point reached in the checkout flow to avoid inconsistencies on interruptions. You can learn more about the idempotency key here. You can also learn how to use it within your strategy by following this guide
  • contextCopy to Clipboard: the third parameter of the method, which is an object that holds a single property ipCopy to Clipboard. ipCopy to Clipboard is a string indicating the IP of the customer.

The completion strategy is expected to return an object with the following properties:

  • response_codeCopy to Clipboard: a number indicating the response code.
  • response_bodyCopy to Clipboard: an object that will be returned to the client.

You can refer to this guide to learn how the cart conceptual guide is implemented in the Medusa backend. This can help you understand how details such as inventory, taxes, and more are handled.


Step 3: Run Build Command

In the directory of the Medusa backend, run the buildCopy to Clipboard command to transpile the files in the srcCopy to Clipboard directory into the distCopy to Clipboard directory:

yarn build
Report Incorrect CodeCopy to Clipboard

Test it Out

Run your backend to test it out:

npx @medusajs/medusa-cli develop
Report Incorrect CodeCopy to Clipboard

Then, try out your strategy using the Complete Cart endpoint. You should see the logic you implemented used for completing the cart.


See Also

Was this page helpful?