@parachutehome/discountler

Purpose

This package provides services for managing and calculating product discounts, including scheduled sales and customer-based discounts.

This package also exports some CRUD utilities for managing discounts and sales (a subset of discounts).

Docs

Docs generated via typedoc live at Vercel here.

Discountler class

The Discountler is the primary utility for interfacing with discounts. You should instantiate a singleton instance for the whole app to gain the benefits of internal caching.

// ~/clients/discountler
import { Discountler } from '@parachutehome/discountler';

export const discountler = new Discountler();

CustomerDiscounter class

The CustomerDiscounter holds the main logic for calculating customer-specific discounts. However, you probably do not want to instantiate one of these directly. Instead, use your instantiated Discountler to generate a CustomerDiscounter instance specific to the customer in question.

import { discountler } from '~/clients/discountler';

export async function getDiscountInfoFromRequest(
request: StorefrontRequest & RequestMaybeWithUserProfile,
) {
const {
userProfile,
storeDomainId,
locale,
userContext: {
utmSource,
utmMedium,
utmCampaign,
utmTerm,
utmContent,
},
} = request;
const customerDiscounter = await discountler.getCustomerDiscounter({
storeDomainId,
email: userProfile?.email,
tags: userProfile?.app_metadata?.tags,
isMember: Boolean(userProfile?.email),
utmSource,
utmMedium,
utmCampaign,
utmTerm,
utmContent,
});
return customerDiscounter;
}

This customerDiscounter now contains only discount information that this customer is eligible for.

Usage

Once you have your customerDiscounter, you can now access specific product discount information. Here it is important to note that discounts can be set up with product restrictions, so it is important to pass in the relevant product information. For example:

const productSpecificDiscount = customerDiscounter?.getDiscount({
tags: someProduct.tags
});

const actualDiscountPercentage = productSpecificDiscount?.discount ?? 0;

Generated using TypeDoc