Skip to content

Coupon for single use can be used multiple times #38017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 of 5 tasks
HenKun opened this issue Sep 28, 2023 · 25 comments
Open
1 of 5 tasks

Coupon for single use can be used multiple times #38017

HenKun opened this issue Sep 28, 2023 · 25 comments
Labels
Area: Order Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: dev in progress Reported on 2.4.6-p2 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch

Comments

@HenKun
Copy link

HenKun commented Sep 28, 2023

Preconditions and environment

  • Magento version 2.4.6-p2
  • Anything else that would help a developer reproduce the bug

Steps to reproduce

  1. Create a cart rule with coupon usage and usage per coupon = 1
    1

  2. Add fixed cart as Action
    2

  3. Generate coupon code (used is no, used times is 0)
    3

  4. Open browser 1, apply coupon code an go to last checkout page
    4

  5. Open browser 2 (or incognito tab), apply coupon code an go to last checkout page
    5

  6. Place order in browser 1 and 2 simultanously (a few seconds might be ok)

  7. Both orders are successfull
    6
    7
    8

  8. Control coupn code usage. It is used and "times used" is 2
    9

Expected result

Coupon can only be used for one order

Actual result

Coupon can be used multiple times, if fast enough

Additional information

Related:

https://experienceleague.adobe.com/docs/commerce-knowledge-base/kb/troubleshooting/miscellaneous/coupon-code-used-more-than-once-adobe-commerce.html?lang=en
#23907
#35077

In this case, the consumer is correctly set up, so the coupon code usage is raised correctly. However, due to the queue mechanism, this is not done fast enough, so a customer can place multiple orders with the same coupon code within the time period the system needs to raise the coupon usage.

This can be exploited by customers, e.g. if coupon codes are used as simple gift cards.

Maybe it isn't a good idea to use queues in this scenario where it relies on an immediate raise of the coupon code usage?
Or a coupon code should already be blocked when it is applied to the quote.

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
@m2-assistant
Copy link

m2-assistant bot commented Sep 28, 2023

Hi @HenKun. Thank you for your report.
To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:


Join Magento Community Engineering Slack and ask your questions in #github channel.
⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.
🕙 You can find the schedule on the Magento Community Calendar page.
📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

@HenKun
Copy link
Author

HenKun commented Sep 28, 2023

@magento give me 2.4-develop instance

@magento-deployment-service
Copy link

Hi @HenKun. Thank you for your request. I'm working on Magento instance for you.

@engcom-Bravo engcom-Bravo self-assigned this Sep 28, 2023
@m2-assistant
Copy link

m2-assistant bot commented Sep 28, 2023

Hi @engcom-Bravo. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
  • 3. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
  • 4. Verify that the issue is reproducible on 2.4-develop branch
    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!
  • 5. Add label Issue: Confirmed once verification is complete.
  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@engcom-Bravo engcom-Bravo added the Reported on 2.4.6-p2 Indicates original Magento version for the Issue report. label Sep 28, 2023
@magento-deployment-service
Copy link

@HenKun
Copy link
Author

HenKun commented Sep 28, 2023

The issue can be reproduced on generated instance.
However:

  • Multiple coupon codes per rule are not generated, I had to use the specific primary coupon code.
  • Fixed amount discount for whole cart isn't working at all, no discount is applied. I had to use Fixed amount discount

Nonetheless, the issue is reproduced under these conditions. Further orders (after both orders are processed) with the same coupon code fail, so it can be concluded the queue works, but is not fast enough.

@achourey
Copy link

achourey commented Oct 3, 2023

Hi @HenKun ,

I am also facing the same issue. Please let me know if you find any solution for this issue.

Thanks in advance

@HenKun
Copy link
Author

HenKun commented Oct 3, 2023

The problem won't be easy to solve. This needs to be done completely or at least partially synchronous. I hope Magento will care for this issue

@m2-assistant
Copy link

m2-assistant bot commented Oct 3, 2023

Hi @engcom-Dash. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

    1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
    1. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
    1. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
    1. Verify that the issue is reproducible on 2.4-develop branch
      Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
      - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
      - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

@engcom-Dash
Copy link
Contributor

@magento give me 2.4-develop instance

@magento-deployment-service
Copy link

Hi @engcom-Dash. Thank you for your request. I'm working on Magento instance for you.

@magento-deployment-service
Copy link

@engcom-Dash
Copy link
Contributor

Hi @HenKun .Thanks for reporting and collaboration. Verified the issue on Magento 2.4-develop instance and the issue is reproducible. Please refer the below screenshots.
Cart rule :
image

image

Order from browser 1
image

Order from browser 2

image

Coupon code usage

image

@engcom-Dash engcom-Dash added Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed and removed Issue: ready for confirmation labels Oct 4, 2023
@github-jira-sync-bot github-jira-sync-bot removed the Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed label Oct 4, 2023
@github-jira-sync-bot
Copy link

Unfortunately, not enough information was provided to create a Jira ticket. Please make sure you added the following label(s): Reproduced on 2.4.x, ^Area:.*

Once all required labels are present, please add Issue: Confirmed label again.

@m2-community-project m2-community-project bot added Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: ready for confirmation labels Oct 4, 2023
@github-jira-sync-bot github-jira-sync-bot removed the Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed label Oct 4, 2023
@github-jira-sync-bot
Copy link

Unfortunately, not enough information was provided to create a Jira ticket. Please make sure you added the following label(s): Reproduced on 2.4.x, ^Area:.*

Once all required labels are present, please add Issue: Confirmed label again.

@engcom-Dash engcom-Dash added Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed and removed Issue: ready for confirmation labels Oct 4, 2023
@github-jira-sync-bot
Copy link

✅ Jira issue https://jira.corp.adobe.com/browse/AC-9649 is successfully created for this GitHub issue.

@m2-assistant
Copy link

m2-assistant bot commented Oct 4, 2023

✅ Confirmed by @engcom-Dash. Thank you for verifying the issue.
Issue Available: @engcom-Dash, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

@engcom-Dash
Copy link
Contributor

@magento give me 2.4-develop instance

@magento-deployment-service
Copy link

Hi @engcom-Dash. Thank you for your request. I'm working on Magento instance for you.

@magento-deployment-service
Copy link

@engcom-Bravo engcom-Bravo added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Oct 9, 2023
@quterorta
Copy link
Contributor

@magento I am working on this

@amenk
Copy link
Contributor

amenk commented May 31, 2024

@engcom-Dash What is the status of this? Is a fix planned?

@amenk
Copy link
Contributor

amenk commented May 31, 2024

The following patch seems to work:

vendor/magento/module-sales-rule/Model/Coupon/Quote$ diff -u UpdateCouponUsages.php.orig UpdateCouponUsages.php
--- UpdateCouponUsages.php.orig 2024-05-31 11:05:21.475190656 +0200
+++ UpdateCouponUsages.php      2024-05-31 11:12:30.899470917 +0200
@@ -8,6 +8,7 @@
 namespace Magento\SalesRule\Model\Coupon\Quote;
 
 use Magento\Quote\Api\Data\CartInterface;
+use Magento\SalesRule\Model\Coupon\Usage\Processor as CouponUsageProcessor;
 use Magento\SalesRule\Model\Coupon\Usage\UpdateInfo;
 use Magento\SalesRule\Model\Coupon\Usage\UpdateInfoFactory;
 use Magento\SalesRule\Model\Service\CouponUsagePublisher;
@@ -33,10 +34,12 @@
      */
     public function __construct(
         CouponUsagePublisher $couponUsagePublisher,
-        UpdateInfoFactory $updateInfoFactory
+        UpdateInfoFactory $updateInfoFactory,
+        CouponUsageProcessor $couponUsageProcessor
     ) {
         $this->couponUsagePublisher = $couponUsagePublisher;
         $this->updateInfoFactory = $updateInfoFactory;
+        $this->couponUsageProcessor = $couponUsageProcessor;
     }
 
     /**
@@ -59,6 +62,7 @@
         $updateInfo->setCustomerId((int)$quote->getCustomerId());
         $updateInfo->setIsIncrement($increment);
 
-        $this->couponUsagePublisher->publish($updateInfo);
+        //$this->couponUsagePublisher->publish($updateInfo);
+        $this->couponUsageProcessor->process($updateInfo);
     }
 }

I guess the original intention do you this async via queue is for very high volume shops? Maybe it could be made configurable or only be queued in such cases, where the usage is not limited.

@engcom-Hotel engcom-Hotel moved this to Ready for Development in High Priority Backlog Aug 19, 2024
@shikharainabounteous
Copy link

any updates on this?

@jaminion
Copy link

jaminion commented May 6, 2025

Hello all, I was just checking on this from an internal issue and it appears they have made adjustments to make the coupon usage update synchronously. For example in the 2.4.5-p11 tagged version, they include a reference to CouponUsageProcessor in the constructor and use the updateCouponUsages() and updateCustomerRulesUsages() methods in the execute(). I'm not quite sure in what context these changes were made as they are not present in the 2.4.5-p10, but in both the 2.4.5-p11 and 2.4.5-p12 tagged versions of the file, the changes are linked to the packaging commit for that tag and no other reference.

https://github.com/magento/magento2/blob/2.4.5-p11/app/code/Magento/SalesRule/Model/Coupon/Quote/UpdateCouponUsages.php

I had been checking this issue and just seeing that it was "open" instead of looking directly at the files. Is this sufficient or are there still timing issues with the implementation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Order Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: dev in progress Reported on 2.4.6-p2 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants