Skip to main content

Badge Plugin

A Vendure plugin for creating image badges and showing them on products in your storefront. A badge is an image asset (e.g. "New", "Sale", "Best Seller") placed at a fixed position on a product image. Badges are attached to collections, and every product in a badged collection automatically inherits that collection's badge, so you label many products at once instead of one at a time.

Badges are managed from the Admin UI and read from the Shop API, are channel-aware (each channel sees only its own badges), and expose a ready-to-use badges field on Product, ProductVariant, and SearchResult.

Features​

  • Image badges – Each badge is an uploaded asset rendered at a chosen position on the product
  • Collection-based – Assign a badge to a collection; all products in that collection (and its sub-collections) inherit it
  • Promotion-based – Or link a badge to a promotion; products that satisfy the promotion's conditions show it while the promotion is active
  • Configurable positions – Restrict badges to a fixed set of positions (e.g. top-left, top-right); invalid positions are rejected
  • Channel-aware – Badges are scoped per channel; admins and storefronts only see badges for the active channel
  • Admin UI – Manage badges from a dedicated section in the Vendure Admin UI / Dashboard
  • Shop API – Read badges directly off Product, ProductVariant, and SearchResult, or query a collection's badge

Compatibility​

Vendure ^3.6.0

Getting started​

npm install @haus-tech/badge-plugin

Or with Yarn:

yarn add @haus-tech/badge-plugin

Configuration​

Add the plugin to your Vendure configuration in vendure-config.ts:

import { BadgePlugin } from '@haus-tech/badge-plugin'

export const config = {
plugins: [
BadgePlugin.init({
availablePositions: ['top-left', 'top-right', 'bottom-left', 'bottom-right'],
}),
],
}

The plugin adds a badge table (plus its channel join table) to the database, so generate and run a migration before starting the server:

npx vendure migrate

(Or generate one with your project's existing migration workflow.) Then restart the server. Badges appear under their own section in the Admin UI.

Upgrading: the badge table gets a new nullable promotionId column (a link to a promotion). Generate and run a migration after updating. Collection and promotion badges are now returned ordered by order, then by id. ProductVariant.badges now uses only public collections, matching Product.badges, so a badge on a private collection no longer shows on variants.

Configuration options​

OptionTypeDefaultDescription
availablePositionsstring[]['top-left', 'top-right', 'bottom-left', 'bottom-right']Available positions selecteble in the ui
promotionProductChecksPromotionProductCheck[][]Extra checks that answer whether a product satisfies a promotion condition (see below)

The configured positions are also exposed on the Admin API via the getBadgePluginConfig query, so the Admin UI and the React Dashboard can offer them as choices.

Promotion product checks​

A promotion badge shows on a product when every condition of the promotion is satisfied by that product. A condition is only checked if a PromotionProductCheck is registered for its code. The plugin ships containsProductsCheck for contains_products (registered by default, the required quantity is ignored). A promotion with a condition that has no check never shows its badge, and the React Dashboard lists the unchecked condition codes.

Register a check for your own condition:

import { BadgePlugin, PromotionProductCheck } from '@haus-tech/badge-plugin'

const inFacetCheck: PromotionProductCheck = {
code: 'my_facet_condition',
async matches({ ctx, injector, productId, args }) {
// args are the condition's arguments, parsed as Vendure parses them
return await myService.productHasFacetValue(ctx, productId, args.facetValueId as string)
},
}

BadgePlugin.init({ promotionProductChecks: [inFacetCheck] })

A check that throws counts as no match and is logged.

The badge follows the promotion's conditions, not its actions. A promotion that limits its discount to some products only through its action (for example products_percentage_discount with no condition) shows the badge on every product. Give it a product condition such as contains_products to scope the badge.

How it works​

A badge holds an image asset, a position and a link to one collection or one promotion, never both. Products are never badged directly, they inherit badges from the collections they belong to or the promotions they satisfy:

  1. Create a badge in the Admin UI: upload an image, pick a position, and assign it to a collection.
  2. Products inherit it – every product in that collection, including products in its sub-collections, gets the badge automatically through the Shop API. A promotion badge is shown on products that satisfy all of the promotion's conditions, while the promotion is enabled and within its dates.
  3. The storefront reads it – fetch the badges field on a product (or search result) and render each badge's image at its position.

Because badges are channel-aware, a badge is only visible in the channel it was created in.

Permissions​

Badge management is exposed on the Admin API and guarded by the standard Vendure catalog permissions:

OperationRequired permission
Query badges / plugin configReadCatalog
Create a badgeCreateCatalog
Update a badgeUpdateCatalog
Delete a badgeDeleteCatalog

The Shop API badge queries are public, matching the rest of the Vendure shop API.

Usage​

Admin UI​

Once installed, manage badges from the dedicated Badges section in the Admin UI. From there you can:

  • Create a badge by uploading an image asset and choosing a position.
  • Assign the badge to a collection or to a promotion (picking one clears the other; the promotion picker is in the React Dashboard). A warning shows when the promotion has conditions that cannot be checked per product.
  • Update or delete existing badges. Deleting a badge also deletes its image asset.

Shop API​

The plugin extends the Shop API with badge queries and adds a badges resolver field to Product, ProductVariant, and SearchResult.

Field / QueryReturnsDescription
Product.badges[Badge!]!Badges inherited from the product's collections and matching active promotions
ProductVariant.badges[Badge!]!Badges from the variant's collections and matching active promotions
SearchResult.badges[Badge!]!Badges for a search result (its product's collections and matching active promotions)
badges(options)BadgeList!Paginated list of all badges in the active channel
getBadgeFromCollection(collectionId)BadgeThe badge assigned to a single collection, if any
getBadgesFromCollections(collectionIds)[Badge!]!Badges assigned to any of the given collections

The most common pattern is to read badges straight off a product:

query ProductBadges($slug: String!) {
product(slug: $slug) {
id
name
badges {
id
position
text
asset {
preview
}
}
}
}

Or off search results, to render badges in a product listing:

query Search($input: SearchInput!) {
search(input: $input) {
items {
productName
badges {
position
asset {
preview
}
}
}
}
}

Example integration​

Fetch a product's badges from the Shop API and render each one at its position:

const res = await fetch('/shop-api', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: `
query ($slug: String!) {
product(slug: $slug) {
badges {
position
text
asset { preview }
}
}
}
`,
variables: { slug: 'my-product' },
}),
})

const { data } = await res.json()
// data.product.badges -> [{ position: 'top-left', asset: { preview } }, ...]
// Render each badge image absolutely positioned over the product image using `position`.

Resources​