Skip to content
New issue

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

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

Already on GitHub? # to your account

Bug Fix: Ensure Limited Usage Coupon is Applied Only Once #22

Conversation

Safemood
Copy link
Owner

This PR addresses the issue of Limited Usage Coupons being mistakenly applied multiple times. The fix involves removing the coupon from the list when it reaches its usage limit, preventing further unintended applications.

Additionally, test cases have been added to validate this behavior. These tests confirm that when a Limited Usage Coupon reaches its usage limit, it is properly removed from the list, ensuring reliable prevention of the bug's recurrence.

it('applies limited usage coupon only once', function () {


    Coupon::add([
        'code' => 'LIMITED50',
        'discount' => 50,
        'usageLimit' => 1, // Limited to 1 uses
        'startDate' => now(),
        'endDate' => now()->addWeek(),
    ]);

 
    $items = [
        [
            "id" => 1,
            "product_id" => 1,
            "product_name" => "Product 1",
            "quantity" => 5,
            "price" => 10
        ],
        [
            "id" => 2,
            "product_id" => 2,
            "product_name" => "Product 2",
            "quantity" => 2,
            "price" => 25
        ]
    ];

    $discountedTotal1 = DiscountifyFacade::setItems($items)
        ->applyCoupon('LIMITED50')
        ->total();

    $appleied1 =  DiscountifyFacade::coupons()->appliedCoupons();


    $discountedTotal2 = DiscountifyFacade::setItems($items)
        ->applyCoupon('LIMITED50')
        ->total();

    $appleied2 =  DiscountifyFacade::coupons()->appliedCoupons();


    expect($discountedTotal1)->toBe(floatval(50));
    expect($appleied1)->not->toBeEmpty();
    expect($appleied1[0]['code'])->toEqual('LIMITED50');

    expect($discountedTotal2)->toBe(floatval(100)); // without the discount
    expect($appleied2)->toBeEmpty();

});

@Safemood Safemood self-assigned this Mar 31, 2024
@Safemood Safemood merged commit 7165873 into main Mar 31, 2024
1 check passed
@Safemood Safemood deleted the 21-bug-limited-usage-coupon-applied-multiple-times-despite-usage-limit-of-1 branch April 7, 2024 09:56
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Limited Usage Coupon Applied Multiple Times Despite Usage Limit of 1
1 participant