Back to Lab
13 min

"The Server Decides the Price" — Wholesale Discounts, Shipping, and Product Visibility in Medusa

In a B2B store where prices vary per wholesale member, how do you make display, billing, and confirmation emails always match? We explain the design we adopted for MU COFFEE — 'the discount rate is applied by the server at billing time' — covering the checkout/init implementation, per-segment shipping, and per-customer product visibility. (Medusa case study series, part 3, finale)

MedusaB2BWholesalePricingCommerce

"The Server Decides the Price" — Designing Wholesale Discounts

The heart of a B2B wholesale store is price. The same product costs different amounts by customer group or individual contract; there are category-level discounts; shipping varies by member type. How you implement this complexity without it falling apart decides the success of a B2B commerce build.

In this final part of the series, we explain the design philosophy and implementation of discounts, shipping, and visibility control adopted in MU COFFEE. See also part 1 (platform selection) and part 2 (deferred payment).

The principle: never accept prices from the frontend

There is a design you must not use for member-specific pricing: trusting a price the frontend computed.

✗ Anti-pattern
Frontend: "This product — I get 20% off, so unit price ¥800."
→ The server believes it and puts it in the cart
→ Tampering, inconsistency, and mismatch with the confirmation email follow

MU COFFEE's principle is the opposite:

The frontend sends only "what and how many." The server decides the price.

The discount rate is derived by the server from the customer's attributes, and at billing time the line item's unit price is recomputed. As a result, on-screen display, the actual charge, and the confirmation email always match. A discounted price may be shown for display, but it never becomes the basis for billing.

Discount-rate priority

The discount rate resolves in the following priority. The shared logic lives in a single file (wholesale-discount.ts), so the display API, the billing process, and email all use the same function.

// Conceptual: deciding the discount rate
// 1. an individual customer's value takes precedence
// 2. then customer group (the max if they belong to several)
// 3. finally the region default
function resolveDiscountRate(customer, region): number {
  const individual = customer.metadata?.discount_rate
  if (individual != null) return Number(individual)

  const groupMax = customer.groups
    .map((g) => Number(g.metadata?.discount_rate ?? 0))
    .reduce((a, b) => Math.max(a, b), 0)
  if (groupMax > 0) return groupMax

  return Number(region.metadata?.default_discount_rate ?? 0)
}

We also support category-level discounts (category_discounts). If a category-level value is specified, it takes precedence over the flat discount. Fine-grained settings like "15% on coffee beans, 5% on equipment" are possible at both the individual and group levels.

checkout/init: the server finalizes the unit price at billing time

The discount actually takes effect in a custom API, POST /store/checkout/init. Here we resolve the authenticated customer's discount rate and the server rewrites each line item's unit price in the cart to the discounted amount.

// Conceptual: applying the unit price in checkout/init
const rate = resolveDiscountRate(customer, region)

for (const item of cart.items) {
  // Skip products excluded from wholesale pricing
  if (item.product.metadata?.wholesale_excluded === true) continue

  const discounted = applyRate(item.original_unit_price, rate, item)
  await cartService.updateLineItem(item.id, { unit_price: discounted })
}

// Shipping is also chosen by the server here (see below)
await applyShipping(cart, customer)

Excluding products from wholesale pricing matters too. Products you don't want discounted — test roasts, subscription items — are excluded via metadata.wholesale_excluded === true. So a discount label isn't shown by mistake on excluded products, the display side reads this metadata as well.

Whether someone is a wholesale member is determined by membership in the customer group named in the MEDUSA_WHOLESALE_GROUP_ID environment variable. In the group → wholesale member: a simple, robust check.

Per-segment shipping: also decided by the server

Shipping follows the same philosophy. We unify it as tax-inclusive and let the server choose from customer attributes.

SegmentShipping (tax incl.)
Retail¥250
Wholesale¥880
Free shipping¥0

The free-shipping threshold resolves hierarchically:

  • General members' shared threshold: region metadata (free_shipping_threshold)
  • Per-company: customer.metadata.free_shipping_threshold (takes precedence over the shared one)
  • Wholesale members have free shipping removed (determined by wholesale-group membership). An individual threshold can restore it

Because checkout/init does this on the server, there is no room for frontend tampering. GET /store/shipping/free-threshold returns the current threshold including the wholesale-member determination, so display and billing don't diverge.

Per-customer product visibility

Beyond price, we also control what is shown per customer. We gave customer.metadata.product_visibility three modes.

  • all (default) — show everything
  • include — show only the specified products
  • exclude — show everything except the specified products
// Concept of GET /store/products/visible
function filterVisible(products, customer) {
  const v = customer?.metadata?.product_visibility
  if (!v || v.mode === "all") return products
  if (v.mode === "include") return products.filter((p) => v.ids.includes(p.id))
  if (v.mode === "exclude") return products.filter((p) => !v.ids.includes(p.id))
  return products
}

Operations like "show a limited product only to this wholesale buyer" or "hide test products from a specific customer" can be configured from the Admin without touching code.

Operations: don't make anyone hand-write metadata

This design relies heavily on metadata, but we were strict about never making operators hand-write JSON. MU COFFEE provides a dedicated "Wholesale Discounts" management screen in the Admin UI, plus widgets on the customer and product editors.

  • Region-wide default discount and free-shipping threshold
  • Per-customer-group discount rate and category-level discounts
  • Per-company discount rate, free-shipping threshold, and product visibility

These are configurable via GUI and land on the appropriate metadata behind the scenes. "Design centralized on the server, operation no-code" is the trick to keeping B2B operations from breaking down.

One philosophy running through the whole design

Everything across these three parts converges on a single idea:

Every important decision — price, shipping, credit, visibility — is made by the server.

  • The discount rate is resolved by the server and applied to the unit price at billing time in checkout/init
  • Shipping is chosen by the server from customer attributes
  • Deferred-payment capture is run automatically by the server, event-driven (part 2)
  • Visibility control is also determined by the server from customer metadata

The frontend just sends "what and how many." This consistency fundamentally prevents the accident most common in B2B commerce — mismatches across display, billing, email, and inventory. And this design freedom is exactly the "reason we chose Medusa" from part 1.

Summary (series finale)

  • For member-specific pricing, the rule is "never trust the frontend's price." The server decides the price
  • Resolve the discount rate as individual > group max > region default, plus category-level, consolidated in shared logic
  • Billing finalizes the unit price on the server in checkout/init. Display, billing, and email match
  • Shipping, free-shipping thresholds, and product visibility are all server-determined, leaving no room for tampering
  • Keep operations entirely in the GUI — never hand-write metadata

Need B2B pricing design, wholesale features, deferred payment, or a headless commerce build — delivered from Japan? Talk to gKnowsis. We support "requirements that off-the-shelf commerce can't bend to," from design through operations, and can build a case like the MU COFFEE example tailored to your requirements.