Skip to content

Prevent unenrollment on order removal when student has another active order - #3483

Open
noruzzamans wants to merge 2 commits into
gocodebox:devfrom
noruzzamans:fix/3464-prevent-unenroll-on-order-removal-with-active-orders
Open

noruzzamans wants to merge 2 commits into
gocodebox:devfrom
noruzzamans:fix/3464-prevent-unenroll-on-order-removal-with-active-orders

Conversation

@noruzzamans

Copy link
Copy Markdown

Description

When an order is trashed or permanently deleted, LLMS_Controller_Orders::error_order() and LLMS_Controller_Orders::on_delete_order() attempt to unenroll the student or delete their enrollment scoped to the specific order trigger (order_<id>).

Previously, if an enrollment had no stored _enrollment_trigger (for example, legacy enrollments, or enrollments imported or created without a trigger), or if an older order was being trashed while a newer active order still existed, LLMS_Student::unenroll() and LLMS_Student::delete_enrollment() fell back to unenrollment/deletion regardless of other existing orders. Consequently, moving an unrelated failed order to the trash or deleting it would cancel or remove enrollment for a student who still had an active or completed order backing the product.

This update:

  • Introduces LLMS_Student::get_active_order_for_product() to determine whether the student has another active (llms-active) or completed (llms-completed) order for the given product, with optional exclusion of the order currently being removed.
  • Introduces LLMS_Student::set_enrollment_trigger() as a helper to persist a new enrollment trigger.
  • In both LLMS_Student::unenroll() and LLMS_Student::delete_enrollment(), before proceeding with unenrollment or deletion, checks if another active or completed order exists for the student and product. If one is found, unenrollment or deletion is skipped and _enrollment_trigger is updated to point to that active order.

Fixes #3464

How has this been tested?

  • Added test_error_order_preserves_enrollment_when_active_order_exists() in class-llms-test-controller-orders.php to verify that trashing an order does not unenroll a student who has another active order for the product.
  • Added test_on_delete_order_preserves_enrollment_when_active_order_exists() in class-llms-test-controller-orders.php to verify that permanently deleting an order preserves enrollment when another active order exists.
  • Added test_error_order_matching_trigger_preserves_enrollment_when_active_order_exists() in class-llms-test-controller-orders.php to verify the edge case where an older order matching the trigger is trashed while a newer active order exists.
  • Added tests in class-llms-test-model-llms-student.php covering get_active_order_for_product(), set_enrollment_trigger(), and unenrollment/deletion guards when trigger is missing.
  • Verified PHPCS against phpcs.xml (0 errors, 0 warnings on new code).

Screenshots

N/A

Types of changes

Bug fix (non-breaking change which fixes an issue)

Checklist:

[x] This PR requires and contains at least one changelog file.
[x] My code has been tested.
[x] My code passes all existing automated tests.
[x] My code follows the LifterLMS Coding & Documentation Standards.

@brianhogg brianhogg moved this to Awaiting Review in Development Nov 13, 2026
@noruzzamans
noruzzamans marked this pull request as ready for review November 13, 2026 17:14
@brianhogg

Copy link
Copy Markdown
Contributor

@noruzzamans Thanks for the PR! Is this something you've observed on an actual site without editing the database? Since you can't purchase the same product multiple times, it's not something we've actually be able to reproduce.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Awaiting Review

Development

Successfully merging this pull request may close these issues.

2 participants