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
- Install the package with Copy to clipboard
yarn add @rsc-labs/medusa-affiliate-discount
or Copy to clipboardnpm i @rsc-labs/medusa-affiliate-discount
. - In Copy to clipboard
medusa-config.js
, add the plugin to the Copy to clipboardplugins
array.
123456789const plugins = [// ... other plugins{resolve: `@rsc-labs/medusa-affiliate-discount`,options: {}}]
- If you are interested also in using simple Admin UI widget, please add Copy to clipboard
enableUI
option.
123456789const plugins = [// ... other plugins{resolve: `@rsc-labs/medusa-affiliate-discount`,options: {enableUI: true}}]
How can I use it?
enableUI enabled
- Create a new discount.Note: Discount shall be assigned only to one region. See Limitations for more info.
- Go to customer details pageNote: Plugins assumes that you start your affiliate program with existing customer in your database.
- On customer details page, you will find below UI widget. Click Copy to clipboard
Create new
- In below screen, choose a discount which you created and define commission in % (0-100).
- Click Copy to clipboard
Publish 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 clipboard
Discount 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 clipboard
Usage count
and Copy to clipboardearnings
are increased when event Copy to clipboardOrder.COMPLETED
is generated.Note: Remember that Copy to clipboard
Order.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 clipboard
Earnings
are calculated based on Copy to clipboardcommission
which you defined previously. The algorithm looks like this:- Take "items" from "order"
- For every "item", take "unit_price", "quantity" and "adjustments".
- Take every "adjustment" which contains "discountId", which is related to "affiliate_discount".
- For every "adjustment" calculate Copy to clipboard
earnings
= Copy to clipboard"unit_price"
- Copy to clipboard
"quantity"
- Copy to clipboard
commission
.
- Copy to clipboard
- Copy to clipboard
- Add new Copy to clipboard
earnings
to existing ones.
Configuration
You can change, when Copy to clipboard
earnings
and Copy to clipboardusage count
are increased by providing Copy to clipboardOrder.PAYMENT_CAPTURED
.12345678910const plugins = [// ... other plugins{resolve: "medusa-affiliate-discount",options: {updateWhen: 'PAYMENT_CAPTURED',enableUI: true}}]
Note: Copy to clipboard
Order.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 clipboard
Order.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:
- Copy to clipboard
Earnings
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. - 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.
- 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. - 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
© 2024 RSC https://rsoftcon.com/