Product Relations
Cross-sells and upsells per product
@topo-js/medusa-plugin-product-relations
Cross-sells and upsells for Medusa 2.23+: hand-picked, ordered product lists per product, edited on the admin product page and served by store routes that return products priced and shaped like core's Copy to clipboard/store/products.
- Cross sell: products that go with this one ("Complete your purchase").
- Upsell: step-up versions or better alternatives ("Upgrade your choice").
Install
1npm install @topo-js/medusa-plugin-product-relations
1234567// medusa-config.tsmodule.exports = defineConfig({// ...plugins: [{ resolve: "@topo-js/medusa-plugin-product-relations", options: {} },],});
1npx medusa db:migrate
Data model
Module Copy to clipboardproductRelation. Tables are prefixed with Copy to clipboardtopojs_ so they never clash with your own.
Copy to clipboardproduct_relation_list (Copy to clipboardtopojs_product_relation_list): one per product and type, unique on Copy to clipboard(product_id, type).
Field Type Notes Copy to clipboardid Copy to clipboardprlist_... Copy to clipboardproduct_id text the product whose page shows the list Copy to clipboardtype Copy to clipboardcross_sell | Copy to clipboardupsell Copy to clipboardtitle text, nullable, translatable null lets the storefront show its own default Copy to clipboarddescription text, nullable, translatable Copy to clipboardmetadata JSON, nullable Copy to clipboardrelations Copy to clipboardproduct_relation[]
Copy to clipboardproduct_relation (Copy to clipboardtopojs_product_relation): a product of a list.
Field Type Notes Copy to clipboardid Copy to clipboardprel_... Copy to clipboardrelated_product_id text Copy to clipboardproduct_relation.product through a read-only link Copy to clipboardrelated_variant_id text, nullable set: only that variant is offered Copy to clipboardrank number order in the list, from 0 Copy to clipboardmetadata JSON, nullable
A deleted product loses its lists and leaves every other product's lists (subscriber on Copy to clipboardproduct.deleted).
Store API
Both routes take the same query params as core's Copy to clipboardGET /store/products/:id (Copy to clipboardfields, Copy to clipboardregion_id, Copy to clipboardcountry_code, Copy to clipboardprovince) and run the same middlewares, so products come with Copy to clipboardcalculated_price, tax amounts and Copy to clipboardinventory_quantity exactly as there, and accept the same Copy to clipboardfields (for example Copy to clipboardfields=*variants.calculated_price,+brand.title). A signed-in customer gets their customer group prices.
A product is suggested only if it is published, sold in the publishable key's sales channel, has a price (when prices are requested), has stock or allows backorders, is not the product itself or already in the cart, and was not suggested earlier in the same response.
Route Query Response Copy to clipboardGET /store/products/:id/relations Copy to clipboardtype (one or both, default both), Copy to clipboardlimit (products in total), Copy to clipboardcart_id Copy to clipboard{ product_relation_lists } Copy to clipboardGET /store/carts/:id/relations Copy to clipboardtype (default Copy to clipboardcross_sell), Copy to clipboardlimit Copy to clipboard{ products }
Copy to clipboard/store/products/:id/relations is for the product page: cross sell then upsell, each with its Copy to clipboardtitle and Copy to clipboarddescription (in the request locale). A list with nothing to sell is left out, so an empty array means "render nothing". With Copy to clipboardcart_id, prices use the cart and its products are never suggested.
123456789101112{"product_relation_lists": [{"id": "prlist_...","type": "cross_sell","title": "Complete your purchase","description": null,"metadata": null,"products": [{ "id": "prod_...", "title": "Serum", "variants": [...] }]}]}
Copy to clipboard/store/carts/:id/relations is for the cart page and the panel after an add to cart: the cross sells of the cart's products, in cart order, without the products already in it. An unknown or completed cart is a 404.
Admin API
Route Description Copy to clipboardGET /admin/products/:id/relations Copy to clipboard{ product_relation_lists }: the product's lists, each with its Copy to clipboardrelations in order and their Copy to clipboardproduct Copy to clipboardPOST /admin/products/:id/relations Saves one list: Copy to clipboard{ type, title?, description?, metadata?, relations: [{ related_product_id, related_variant_id?, metadata? }], bidirectional?, additional_data? }
Copy to clipboardPOST replaces the list's products (the array order is the rank); Copy to clipboardtitle, Copy to clipboarddescription and Copy to clipboardmetadata left out keep their value, Copy to clipboardnull or an empty text clears them. Copy to clipboardbidirectional (cross sell only) also adds this product to the end of each related product's cross sells, unless it is already there. It fails with a 400, writing nothing, on a product related to itself, a product listed twice, an unknown product or a variant of another product.
Admin
On the product page (Copy to clipboardproduct.details.after), one section per list, Cross sell then Upsell, with the list's title (or "Storefront default"), description and products in order (draft products and variant-only relations are flagged). Each section's menu has:
- Edit title and description: a drawer with Copy to clipboard
Formfrom Copy to clipboard@medusajs/dashboard/components, validated with Zod. The title and description are translatable in Settings > Translations. - Edit products: a focus modal with the dashboard's product table (search, pagination, checkboxes) and the selected products beside it, reordered by dragging (mouse or keyboard). On cross sells, a switch also adds this product to the selected products' cross sells (Copy to clipboard
bidirectional).
UI texts in English and Spanish (Copy to clipboardsrc/admin/i18n), keys under Copy to clipboardtopojsProductRelations.*.
Workflows
Exported from Copy to clipboard@topo-js/medusa-plugin-product-relations/workflows. Each step compensates on failure.
Workflow Hook Hook input Copy to clipboardupdateProductRelationsWorkflow Copy to clipboardproductRelationsUpdated Copy to clipboard{ product_relation_list, additional_data } Copy to clipboarddeleteProductRelationsWorkflow
Extend
Add your own data the way Medusa extends products: validate Copy to clipboardadditional_data on Copy to clipboardPOST /admin/products/:id/relations and save it in the workflow hook.
12345678910111213// src/api/middlewares.tsimport { defineMiddlewares } from "@medusajs/framework/http";import { z } from "@medusajs/framework/zod";export default defineMiddlewares({routes: [{matcher: "/admin/products/:id/relations",methods: ["POST"],additionalDataValidator: { campaign: z.string().optional() },},],});
1234567891011// src/workflows/hooks/product-relations-updated.tsimport { updateProductRelationsWorkflow } from "@topo-js/medusa-plugin-product-relations/workflows";import { StepResponse } from "@medusajs/framework/workflows-sdk";updateProductRelationsWorkflow.hooks.productRelationsUpdated(async ({ product_relation_list, additional_data }, { container }) => {// e.g. save additional_data.campaign in a module of yours, linked to// product_relation_list.id, and return what the compensation needs.return new StepResponse(undefined, undefined);},);
Development
123456npm installnpm run build # medusa plugin:build, into .medusa/servernpm run lintnpm run typechecknpm run test:integration:http # needs Postgres, see belownpx medusa plugin:db:generate # after changing a model
The integration tests boot a bare Medusa app (Copy to clipboardintegration-tests/app/medusa-config.ts) that loads the plugin from its build, so run Copy to clipboardnpm run build first. They create their own database with Copy to clipboardDB_HOST, Copy to clipboardDB_PORT, Copy to clipboardDB_USERNAME and Copy to clipboardDB_PASSWORD (default Copy to clipboardpostgres on Copy to clipboardlocalhost:5432).
To try it in a Medusa app while developing, use Copy to clipboardnpx medusa plugin:publish here and Copy to clipboardnpx medusa plugin:add @topo-js/medusa-plugin-product-relations in the app, then Copy to clipboardnpm run dev here to rebuild on change.
Release
12npm version minor # or patch / majorgit push --follow-tags # the Release workflow publishes the tag to npm
The workflow publishes with npm Trusted Publishing, no token: the package's settings on npmjs.com trust this repository and Copy to clipboardrelease.yml.
License
MIT
