Files
medusa-store/www/apps/docs/content/modules/carts-and-checkout/backend/cart-completion-strategy.md
Shahed Nasser fa7c94b4cc docs: create docs workspace (#5174)
* docs: migrate ui docs to docs universe

* created yarn workspace

* added eslint and tsconfig configurations

* fix eslint configurations

* fixed eslint configurations

* shared tailwind configurations

* added shared ui package

* added more shared components

* migrating more components

* made details components shared

* move InlineCode component

* moved InputText

* moved Loading component

* Moved Modal component

* moved Select components

* Moved Tooltip component

* moved Search components

* moved ColorMode provider

* Moved Notification components and providers

* used icons package

* use UI colors in api-reference

* moved Navbar component

* used Navbar and Search in UI docs

* added Feedback to UI docs

* general enhancements

* fix color mode

* added copy colors file from ui-preset

* added features and enhancements to UI docs

* move Sidebar component and provider

* general fixes and preparations for deployment

* update docusaurus version

* adjusted versions

* fix output directory

* remove rootDirectory property

* fix yarn.lock

* moved code component

* added vale for all docs MD and MDX

* fix tests

* fix vale error

* fix deployment errors

* change ignore commands

* add output directory

* fix docs test

* general fixes

* content fixes

* fix announcement script

* added changeset

* fix vale checks

* added nofilter option

* fix vale error
2023-09-21 20:57:15 +03:00

4.4 KiB
Raw Blame History

description, addHowToData
description addHowToData
Learn how to override the cart completion strategy to implement your custom cart completion strategy. true

How to Override Cart Completion Strategy

In this document, youll learn how to override the cart completion strategy.

:::note

This guide only explains how to override the cart completion strategy. Its 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/strategies of your Medusa backend project with a class that extends the AbstractCartCompletionStrategy class:

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

The class includes the complete method defined as abstract in AbstractCartCompletionStrategy. 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:

// ...
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

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


Step 2: Implement the complete Method

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

The method accepts three parameters:

  • cartId: the first parameter of the method, which is a string indicating the ID of the cart to complete.
  • idempotencyKey: the second parameter of the method, which is an instance of the IdempotencyKey entity. The idempotency key is retrieved based on the idempotency key passed in the header of the request, and its 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
  • context: the third parameter of the method, which is an object that holds a single property ip. ip is a string indicating the IP of the customer.

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

  • response_code: a number indicating the response code.
  • response_body: 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 build command to transpile the files in the src directory into the dist directory:

npm run build

Test it Out

Run your backend to test it out:

npx medusa develop

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


See Also