Built by

RSC-Labs

Category

Other

Version

0.1.1

Last updated

Mar 6, 2024, 18:35:27 PM5 months ago

Medusa Affiliate Discount

Medusa "Affiliate Discount" is a plugin which enables possibility to create a discount which is a part of affiliation program.

Why?

Affiliate marketing contains different activities. One of them is affiliation program, so a process which allows other entities (companies, people) to promote products in exchange for commission on each sale. "Affiliate Discount" provides possibility to establish such cooperation and monitor usage count and earnings.

Getting Started

  1. Install the package with Copy to clipboardyarn add @rsc-labs/medusa-affiliate-discount or Copy to clipboardnpm i @rsc-labs/medusa-affiliate-discount.
  2. In Copy to clipboardmedusa-config.js, add the plugin to the Copy to clipboardplugins array.
const plugins = [
// ... other plugins
{
resolve: `@rsc-labs/medusa-affiliate-discount`,
options: {
}
}
]
  1. If you are interested also in using simple Admin UI widget, please add Copy to clipboardenableUI option.
const plugins = [
// ... other plugins
{
resolve: `@rsc-labs/medusa-affiliate-discount`,
options: {
enableUI: true
}
}
]

How can I use it?

enableUI enabled

  1. Create a new discount.
    Note: Discount shall be assigned only to one region. See Limitations for more info.
  2. Go to customer details page
    Note: Plugins assumes that you start your affiliate program with existing customer in your database.
  3. On customer details page, you will find below UI widget. Click Copy to clipboardCreate new
  1. In below screen, choose a discount which you created and define commission in % (0-100).
  1. Click Copy to clipboardPublish affiliate discount

enableUI disabled

Follow above steps, but using API defined here: API

How it works?

Your "affiliate" (customer to which you assigned a discount) can now share Copy to clipboardDiscount Code to promote your products. When other customers will use provided discount code in their carts, then Copy to clipboardusage count and Copy to clipboardearnings are calculated for created Copy to clipboardaffiliate discount.

Details

Copy to clipboardUsage count and Copy to clipboardearnings are increased when event Copy to clipboardOrder.COMPLETED is generated.
Note: Remember that Copy to clipboardOrder.COMPLETED is an event which you need to implement - it does not come by default from Medusa or it cannot be even generated from Admin UI.
You can change event on which these parameters are calculated - see Configuration
Copy to clipboardEarnings are calculated based on Copy to clipboardcommission which you defined previously. The algorithm looks like this:
  1. Take "items" from "order"
  2. For every "item", take "unit_price", "quantity" and "adjustments".
  3. Take every "adjustment" which contains "discountId", which is related to "affiliate_discount".
  4. For every "adjustment" calculate Copy to clipboardearnings = Copy to clipboard"unit_price"
    • Copy to clipboard"quantity"
      • Copy to clipboardcommission.
  5. Add new Copy to clipboardearnings to existing ones.

Configuration

You can change, when Copy to clipboardearnings and Copy to clipboardusage count are increased by providing Copy to clipboardOrder.PAYMENT_CAPTURED.
const plugins = [
// ... other plugins
{
resolve: "medusa-affiliate-discount",
options: {
updateWhen: 'PAYMENT_CAPTURED',
enableUI: true
}
}
]
Note: Copy to clipboardOrder.PAYMENT_CAPTURED is the ONLY possible value here. If not set, Copy to clipboardOrder.COMPLETED is taken by default. This assumption comes from the fact, that Copy to clipboardearnings calculation cannot be reverted, so any other event can be easily exploited by the customer.
WARNING: Please remember that plugin uses "eventName" to distinguish Copy to clipboardOrder.PAYMENT_CAPTURED and Copy to clipboardOrder.COMPLETED. It means to have it fully functional you need to have e.g. Redis Event Module - it won't work properly with Local Event Module. In such case calculation of Copy to clipboardearnings will be done twice (for Copy to clipboardOrder.PAYMENT_CAPTURED and Copy to clipboardOrder.COMPLETED - if you have Copy to clipboardOrder.COMPLETED implemented), so please be careful!

Limitations

As this is just the beginning, here are current limitations:
  1. Copy to clipboardEarnings is simple calculation, but to keep it simple, we need to assume that there is only one currency. In case of international buyings, calculation of Copy to clipboardearnings won't be trivial. One currency is guaranteed by having a discount related to one region which can have only one currency.
  2. For simplicity - affiliate discount cannot be edited. It means that if you want to reset earnings (e.g. when you pay your customer), then you need to delete affiliate discount and create new one. For some scenarios it can be even a better choice, for instance when your discount expired and you would like to create new one.
  3. Admin UI widget is very simple - it might not cover all corner case in terms of validation of fields. One of the reason is that we still do not have experience in HTML/CSS. The other reason is that we are using basic stuff from Copy to clipboard@medusajs/ui, which is heavily limited. If Copy to clipboard@medusajs/ui will be improved, then improvements could be ported here.
  4. The only commission type which is now supported is "percentage". If there will be interest about this plugin, then it might be improved.

TODO

[] Tests
[] Limit limitations :)
[] Develop full affiliate marketing - in case of interest, please contact.

Contribution

Contributions are welcome and they are greatly appreciated!

License

MIT

Build your own plugins

Develop your own plugins with our API to speed up your processes.

Make your plugin available via npm for it to be shared in our Plugin Library with the broader Medusa community.