medusa
medusa copied to clipboard
feat(medusa): Discount limit per customer
What - what changes are in this PR
Added a discount usage limit per customer.
Why - why are these changes relevant
The added field enables setting a usage limit on a discount code per customer. Customers can only use a specific discount code an X amount of times.
How - how have the changes been implemented
- The changes have been implemented by adding a new field to the Discount model: usage_limit_per_customer.
- Added a new method to check how many orders a customer has, with the given discount code.
- Added the method in the validateDiscountForCartOrThrow method.
- Made a migration to add the usage_limit_per_customer field on the Discount table.
Testing - how has the changes been tested or how can the reviewer test the feature?
- Written unit tests for several scenarios
Someone is attempting to deploy a commit to the medusajs Team on Vercel.
A member of the Team first needs to authorize it.
⚠️ No Changeset found
Latest commit: 0f3b96b0046b82155726a22a89be9b5158500238
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
Thanks for this - great contribution. Can you please add a few integration tests :)
If you try adding a discount before the customer is associated with the cart this is thrown:

Not immediately clear how we can get around this. Two ideas come to mind:
- We always throw an error when trying to add a "customer limited" discount before the customer is known.
- We add a discount validation step before completing the cart to check if the discount is valid for the customer.
Both would require storefront implementations to inform customers what is going on.
Do you have any other ideas?
@srindom I have added a check to see if a customer is on a cart, if there is not customer on the cart, the hasCustomerReached method will return false. When trying to apply a discount on a cart where the customer has reached the limit, an error is thrown and the discount code will be removed from the cart.
@srindom could you verify that the approach made by @DidierGuyon is valid choice or do you prefer another option?
@DidierGuyon – could I get you to pull upstream changes? :)
Tests are still failing. Can I get you to fix those?
Tests are still failing. Can I get you to fix those?
Ofcourse, the tests are passing now. @olivermrbl
Any movement on this? I have a request to add a discount code with a limit of 1 use per customer. Seems like this would be a great fit.
Any update on this feature? I just upgraded a project to 1.17 and see that the limit is still global in the admin; there is no per customer limit option. I stepped away from the project for a bit and had hoped maybe this found its way into the release, but apparently not?
️✅ There are no secrets present in this pull request anymore.
If these secrets were true positive and are still valid, we highly recommend you to revoke them. Once a secret has been leaked into a git repository, you should consider it compromised, even if it was deleted immediately. Find here more information about risks.
🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.
Our GitHub checks need improvements? Share your feedbacks!
@olivermrbl In the latest commit, I have added the open points. Invalid discounts get removed from the cart, there is also a integration test for this scenario.
@olivermrbl @DidierGuyon what's the state of this PR? Our team would benefit greatly from this feature! 🙏🏼
@olivermrbl any remarks on the PR, we have updated the branch with latest changes
up
Hey, thanks for the PR! Since v2 brought a lot of architectural and API changes on the backend, we will be closing this ticket.
@riqwan @olivermrbl will Medusa v2 support limiting discounts on customer level out-of-the-box?