Skip to content

[Bug]: Invalid cart total and items price for customers with customer_group #13990

@Infrared96

Description

@Infrared96

Package.json file

{
  "name": "test_medusa",
  "version": "0.0.1",
  "description": "A starter for Medusa projects.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "scripts": {
    "build": "medusa build",
    "start": "medusa start",
    "dev": "medusa develop",
    "migrate": "medusa db:migrate",
    "lint": "eslint src --ext .ts,.tsx",
    "lint:fix": "eslint src --ext .ts,.tsx --fix"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "2.11.3",
    "@medusajs/cli": "2.11.3",
    "@medusajs/framework": "2.11.3",
    "@medusajs/medusa": "2.11.3",
    "@opentelemetry/exporter-trace-otlp-http": "0.53.0",
    "@types/fhir": "^0.0.41",
    "axios": "^1.8.4",
    "moment": "^2.30.1",
    "uuid": "^11.1.0"
  },
  "devDependencies": {
    "@eslint/js": "^9.15.0",
    "@medusajs/test-utils": "2.11.3",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/jest": "^29.5.13",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "eslint": "^9.15.0",
    "eslint-config-prettier": "^9.1.0",
    "eslint-plugin-import": "^2.32.0",
    "eslint-plugin-only-warn": "^1.1.0",
    "eslint-plugin-prettier": "^5.5.4",
    "jest": "^29.7.0",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "typescript-eslint": "^8.15.0",
    "vite": "^5.2.11",
    "yalc": "^1.0.0-pre.53"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

v22.15.0

Database and its version

PostgreSQL 14.19

Operating system name and version

MacOS Sequoia 15.7.1

Browser name

Google Chrome

What happended?

I encountered an issue in version v2.11.* where I can’t create a cart that automatically calculates prices based on the customer_group the customer belongs to.

For example, I create a cart by passing the customer’s email to the API:

Next.js server code:

const { cart } = await medusaSdk.store.cart.create(
  {
    region_id: process.env.MEDUSA_REGION_ID,
    email: customerEmail,
    promo_codes: discountCode ? [discountCode] : [],
    items: [
      {
        variant_id: variant?.id || '',
        quantity: 1,
      },
    ],
  },
  {},
  headers
);

The customer belongs to a group where the product price differs from the default, but the returned price is still the default one.

P.S. In version 2.10.3, we used a small workaround with a medusa hook:

createCartWorkflow.hooks.setPricingContext(
  async ({ customerData }, { container }) => {
    const query = container.resolve(ContainerRegistrationKeys.QUERY);
    const {
      data: [customer],
    } = await query.graph({
      entity: "customer",
      fields: ["*", "groups.*"],
      filters: {
        email: customerData.email,
      },
    });

    if (customer.groups) {
      return new StepResponse({
        customer: {
          groups: {
            id: customer.groups.map((group) => group?.id).filter((id) => id),
          },
        },
      });
    }

    return new StepResponse({});
  },
);

However, this no longer works because the customer field is now being overwritten in your code (line 113):

const cartPricingContext = transform(
{
cart: input.cart,
items: input.items,
setPricingContextResult: input.setPricingContextResult,
},
(
data
): {
id: string
variantId: string
context: Record<string, unknown>
}[] => {
const cart = data.cart
const baseContext = {
...filterObjectByKeys(cart, cartFieldsForPricingContext),
...(data.setPricingContextResult ? data.setPricingContextResult : {}),
currency_code: cart.currency_code ?? cart.region?.currency_code,
region_id: cart.region_id,
region: cart.region,
customer_id: cart.customer_id,
customer: cart.customer,
}
return (data.items ?? cart.items ?? [])
.filter((i) => i.variant_id)
.map((item) => {
const idLike =
(item as CartLineItemDTO).id ?? simpleHash(JSON.stringify(item))
return {
id: idLike,
variantId: item.variant_id!,
context: {
...baseContext,
quantity: item.quantity,
},
}
})
}
)

Expected behavior

The product prices in the cart should be calculated based on the customer group.

Actual behavior

The product prices in the cart can't be calculated by customer group.

Link to reproduction repo

none

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions