medusa icon indicating copy to clipboard operation
medusa copied to clipboard

feat(medusa): Discount limit per customer

Open DidierGuyon opened this issue 1 year ago • 16 comments

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

DidierGuyon avatar Apr 04 '23 09:04 DidierGuyon

Someone is attempting to deploy a commit to the medusajs Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Apr 04 '23 09:04 vercel[bot]

⚠️ 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

changeset-bot[bot] avatar Apr 04 '23 09:04 changeset-bot[bot]

Thanks for this - great contribution. Can you please add a few integration tests :)

srindom avatar Apr 13 '23 08:04 srindom

If you try adding a discount before the customer is associated with the cart this is thrown:

CleanShot 2023-04-13 at 11 00 26@2x

Not immediately clear how we can get around this. Two ideas come to mind:

  1. We always throw an error when trying to add a "customer limited" discount before the customer is known.
  2. 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 avatar Apr 13 '23 09:04 srindom

@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.

DidierGuyon avatar Apr 18 '23 14:04 DidierGuyon

@srindom could you verify that the approach made by @DidierGuyon is valid choice or do you prefer another option?

pepijn-vanvlaanderen avatar May 19 '23 09:05 pepijn-vanvlaanderen

@DidierGuyon – could I get you to pull upstream changes? :)

olivermrbl avatar Jun 26 '23 07:06 olivermrbl

Tests are still failing. Can I get you to fix those?

olivermrbl avatar Jul 24 '23 10:07 olivermrbl

Tests are still failing. Can I get you to fix those?

Ofcourse, the tests are passing now. @olivermrbl

DidierGuyon avatar Jul 25 '23 10:07 DidierGuyon

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.

ripvannwinkler avatar Aug 11 '23 16:08 ripvannwinkler

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?

ripvannwinkler avatar Nov 02 '23 14:11 ripvannwinkler

️✅ 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!

gitguardian[bot] avatar Jan 29 '24 15:01 gitguardian[bot]

@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.

DidierGuyon avatar Jan 29 '24 15:01 DidierGuyon

@olivermrbl @DidierGuyon what's the state of this PR? Our team would benefit greatly from this feature! 🙏🏼

erikengervall avatar Mar 05 '24 01:03 erikengervall

@olivermrbl any remarks on the PR, we have updated the branch with latest changes

pepijn-vanvlaanderen avatar Mar 08 '24 14:03 pepijn-vanvlaanderen

up

alessioacella avatar Mar 29 '24 11:03 alessioacella

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 avatar Jul 05 '24 10:07 riqwan

@riqwan @olivermrbl will Medusa v2 support limiting discounts on customer level out-of-the-box?

pepijn-vanvlaanderen avatar Jul 08 '24 05:07 pepijn-vanvlaanderen