npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@webbers/order-gift-promotions-medusa

v1.0.4

Published

Add order gift promotions functionality for Medusa v2

Readme

Order Gift Promotions for Medusa v2

A Medusa v2 plugin that automatically adds free gift items to a customer's cart based on their order history. When a customer is about to place their Nth order, matching gift promotions are applied to the cart.

How It Works

Automatic Gift Item(s)

  1. An admin creates an order gift promotion that targets a specific order count (e.g., "on a customer's 3rd order")
  2. Each promotion has one or more gift items (product variants with quantities)
  3. When a cart is created or updated, call refreshGiftItems to evaluate eligibility:
    • The customer's completed order history is counted (orders with payment status captured or partially_refunded)
    • If completed orders + 1 matches a promotion's target quantity, the gift items are added to the cart for free
    • Gift items are kept in sync: removed when no longer applicable, quantities corrected if changed

Disable/Enable Automatic Gift Item(s) on Cart

If you want to allow customers to remove gift items from their cart:

  1. Set ogp_disabled: true in the cart's metadata. This will cause refreshGiftItems to skip gift syncing and remove any existing gift items for that cart.
  2. Whenever you want to allow customers to add gift items back to their cart, remove ogp_disabled from the cart's metadata.

Installation

npm install @webbers/order-gift-promotions-medusa

Requirement: Medusa 2.13.5 or higher.

Setup

1. Register the module

In your Medusa backend's medusa-config.ts:

module.exports = defineConfig({
  projectConfig: {
    // ...
  },
  plugins: [
    // ... other plugins
    '@webbers/order-gift-promotions-medusa'
  ],
})

2. Run migrations

npx medusa db:migrate

3. Wire up cart refreshing

Call refreshGiftItems in the beforeRefreshingPaymentCollection hook. Example:

import {refreshCartItemsWorkflow} from "@medusajs/medusa/core-flows"
import {
  refreshCartFields,
  RefreshCartProps,
  refreshGiftItems,
} from "@webbers/order-gift-promotions-medusa/utils"
import {ContainerRegistrationKeys, Modules} from "@medusajs/framework/utils"

refreshCartItemsWorkflow.hooks.beforeRefreshingPaymentCollection(
  async ({input}, {container}) => {
    const query = container.resolve(ContainerRegistrationKeys.QUERY)

    const {
      data: [cart],
    } = await query.graph(
      {
        entity: Modules.CART,
        fields: refreshCartFields,
        filters: {
          id: input.cart_id,
        },
      },
      {
        throwIfKeyNotFound: true,
      }
    )

    const typedCart = cart as unknown as RefreshCartProps

    await refreshGiftItems(
      {
        cart: typedCart,
      },
      container
    )
  }
)

Admin UI

The plugin registers an admin UI page at /promotions/order-gift-promotions where you can:

  • View all order gift promotions
  • Create a new promotion (set the target order count and gift items)
  • Edit an existing promotion
  • Delete a promotion

API Endpoints

All endpoints require admin authentication.

| Method | Path | Description | |--------|------|-------------| | GET | /admin/order-gift-promotions | List all promotions | | POST | /admin/order-gift-promotions | Create a promotion | | GET | /admin/order-gift-promotions/:id | Get a single promotion | | POST | /admin/order-gift-promotions/:id | Update a promotion | | DELETE | /admin/order-gift-promotions/:id | Delete a promotion |

Create/Update payload

{
  "order_quantity": 3,
  "gift_items": [
    {
      "variant_id": "variant_01ABC...",
      "quantity": 1
    }
  ]
}

Development

pnpm install
pnpm dev     # develop mode
pnpm build   # build plugin

License

MIT — Webbers B.V.