Medusa Pricing Groups
What is it?
Medusa Pricing Groups is a plugin for the Medusa headless commerce system that enables one to group products together
for the purposes of price calculation.
Quick Start
- Install the plugin by running Copy to clipboard
npm i -S medusa-products-pricing-groups
. - Add the following to Copy to clipboard
medusa-config.js
to the Copy to clipboardplugins
array: Copy to clipboardjavascript { resolve: `medusa-products-pricing-groups`, options: { enableUI: true } }
- Run Copy to clipboard
npx medusa migrations run
- Start the medusa backend
Once installed, you will see a new entry , Copy to clipboard
Pricing Groups
in the sidebar. From here you can create pricing groups,
add and remove products (click on the newly created group to manage the products).The plugin will also add an admin widget to the product details view. This
widget will show which pricing groups a product is currently associated with.
What problem does it solve?
Out of the box, Medusa has the capability of creating prices for variants of a product in multiple currencies, at
multiple different levels, through the use of Copy to clipboard
fingers crossed it appears in the dashboard UI soon).
min_quantity
& Copy to clipboardmax_quantity
(only possible via API requests currently,fingers crossed it appears in the dashboard UI soon).
Pricing flexibility is further enabled by price lists, which allow one
to set custom prices depending on the audience.
The problem arises when one has a set of products that when purchased together should each contribute towards the
volume discounts of the other products in the cart.
Use Case & Example
Criteria
Let's say we have 10 different t-shirts, in varying colours and sizes.
We have a storefront, which retail customers can access and purchase from. We also have two other audiences
that will be using the storefront; small quantity resellers, and high volume resellers.
Each of these audiences have their own terms and price lists respectively.
Each variant of t-shirt has volume price breaks.
Each t-shirt variant can have different prices.
For the sake of the example we will assume the following prices apply:
For retail customers:
- 1-3 t-shirts: 20
- 3+ t-shirts: 18
For the small quantity & high volume reseller audiences:
- 1-5 t-shirts: 20
- 5-10 t-shirts: 15
- 10+ shirts: 12
All t-shirts cost the same in the example, though in practice the prices can differ, and be set as normal.
Requirements
What we are looking to achieve is the following criteria:
- Each product variant can have its prices set independently in all supported currencies
- Products can easily be grouped and ungrouped
- Products can have different volume breakpoints (i.e. Copy to clipboard
min_quantity
/Copy to clipboardmax_quantity
thresholds can differ between grouped products) - Any applicable volume discounts should be automatically applied
- If multiple discounts are available for a set of products in the cart the best discount should be chosen.
The Example
We have our example case & criteria set out above.
The following are the expected cart totals:
- A retail customer adds Copy to clipboard
1 x red
& Copy to clipboard1 x blue
to their cart.- Pricing tier: Copy to clipboard
1-3 t-shirts
- Cart total: 40 (20 + 20)
- Pricing tier: Copy to clipboard
- A retail customer adds Copy to clipboard
1 x red
, Copy to clipboard1 x green
& Copy to clipboard2 x purple
to their cart.- Pricing tier: Copy to clipboard
3+ t-shirts
- Cart total: 72 (18 + 18 + 36)
- Pricing tier: Copy to clipboard
- A small reseller adds Copy to clipboard
1 x red
, Copy to clipboard1 x green
& Copy to clipboard2 x purple
to their cart.- Pricing tier: Copy to clipboard
1-5 t-shirts
- Cart total: 80 (20 + 20 + 40)
- Pricing tier: Copy to clipboard
- A small reseller adds Copy to clipboard
4 x blue
, Copy to clipboard4 x green
, Copy to clipboard3 x purple
to their cart.- Pricing tier: Copy to clipboard
10+ t-shirts
- Cart total: 132 (48 + 48 + 36)
- Pricing tier: Copy to clipboard
The problem can be partially solved with promotional codes, though this becomes a complexity nightmare, in terms of
tracking active codes, calculating fixed/percentage discounts, monitoring edge cases, and so on. It quickly becomes
impossible to achieve pricing structures due to the rigidity of fixed/percentage discounts, whilst also being a
poor user experience in terms of the user having to choose & manually apply one of potentially many codes.
How this plugin solves it
The concept of a pricing group is introduced. A pricing group is a very simple structure, comprised of an identifier
and a name, along with a set of products tagged with the identifier.
The plugin implements a pricing group service that retrieves
these linkages between the pricing group and products.
The plugin implements a pricing strategy that calculates
and returns the best price for the variant in question.
In simple terms we do the following:
- Retrieve all variants in the cart.
- Iterate all variants, and retrieve associated groups for each variant.
- Create a map of Copy to clipboard
Variant ID => [Group IDs]
- Create a map of Copy to clipboard
Group ID => Calculated Quantity
- Iterate variants in cart, increment Copy to clipboard
Calculated Quantity
for each Copy to clipboardGroup ID
associated with current Copy to clipboardVariant ID
by Copy to clipboardVariant Quantity
- Use Copy to clipboard
Calculated Quantity
to retrieve the Copy to clipboardBest Price
available. - Calculate line item total as Copy to clipboard
Variant Quantity x Best Price
.
N.B. If the cart is undefined for the request, we fall back to retrieving the price for the variant as if it were a
standalone product rather than a grouped one.
Development Notes
When developing the package, attempting to run this plugin on a medusa backend will cause issues if you install
Copy to clipboard
@medusajs/medusa
into the Copy to clipboardnode_modules
folder of the plugin directory, due to dependency conflicts with the medusa
backend.To workaround this:
- Copy to clipboard
cd
into Copy to clipboardnode_modules/@medusajs/medusa
from the root of the medusa backend directory, and run Copy to clipboardyarn link
. - Copy to clipboard
cd
into the plugin directory, then run Copy to clipboardyarn link @medusajs/medusa
. The plugin will now build using the Copy to clipboard@medusajs/medusa
dependency from the backend. - Run Copy to clipboard
yarn link
in the root of the plugin directory. - Copy to clipboard
cd
into the backend directory again, and run Copy to clipboardyarn link medusa-products-pricing-groups
. - You can now run Copy to clipboard
yarn dev
to start the backend, make some changes to the plugin, and run Copy to clipboardyarn build
in the plugin directory to see the changes live reloaded on the backend dashboard UI.
Afterthought / Disclaimer
I am fully aware the code quality isn't perfect as things currently stand - this was somewhat of a proof-of-concept
project to familiarise myself with Medusa and see if it would suit our needs.
There may be bugs, I haven't fully tested this plugin in production.
Also, the data table elements were lifted from the core admin dashboard and modified, as they more or less suited my needs
and I wasn't about to write them from scratch.