Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions inc/functions/membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,27 @@ function wu_membership_create_new_payment($membership, $should_cancel_pending_pa
$new_payment->remove_non_recurring_items();
}

/**
* Fires before a membership payment has its totals recalculated.
*
* Addons can add auditable line items to renewal payments without
* changing the membership's recurring amount or invoking a gateway.
*
* @since 2.15.1
*
* @param \WP_Ultimo\Models\Payment $new_payment Unsaved payment object.
* @param Membership $membership Membership being billed.
* @param bool $remove_non_recurring Whether non-recurring items were removed.
* @param bool $save Whether the payment will be saved.
*/
do_action(
'wu_membership_new_payment_pre_totals',
$new_payment,
$membership,
$remove_non_recurring,
$save
);

$new_payment->recalculate_totals();

if ( ! $save) {
Expand Down
2 changes: 1 addition & 1 deletion inc/models/class-membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ public function swap($order) {
* If that's the case, we need to replace the current
* plan id.
*/
if ($product->get_type() === 'plan') {
if (wu_is_plan_type($product->get_type())) {
$this->set_plan_id($product->get_id());

continue;
Expand Down
79 changes: 79 additions & 0 deletions tests/WP_Ultimo/Functions/Membership_Functions_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,85 @@ public function test_membership_create_new_payment_no_save(): void {
$this->assertEmpty($payment->get_id());
}

/**
* Test addons can add renewal line items before totals are calculated.
*/
public function test_membership_create_new_payment_exposes_pre_totals_action(): void {

$user_id = self::factory()->user->create();

while (wu_get_customer_by_user_id($user_id)) {
$user_id = self::factory()->user->create();
}

$customer = wu_create_customer([
'user_id' => $user_id,
'skip_validation' => true,
]);

$this->assertNotWPError($customer);

$product = wu_create_product([
'name' => 'Extensible Renewal Plan',
'slug' => 'extensible-renewal-plan-' . wp_rand(),
'type' => 'plan',
'amount' => 49.00,
'recurring' => true,
'duration' => 1,
'duration_unit' => 'month',
'skip_validation' => true,
]);

$this->assertNotWPError($product);

$membership = wu_create_membership([
'customer_id' => $customer->get_id(),
'plan_id' => $product->get_id(),
'status' => 'active',
'amount' => 49.00,
'initial_amount' => 49.00,
'currency' => 'USD',
'recurring' => true,
'duration' => 1,
'duration_unit' => 'month',
'skip_validation' => true,
]);

$this->assertNotWPError($membership);

$context = [];
$callback = static function ($payment, $hook_membership, $remove_non_recurring, $save) use (&$context): void {
if ($hook_membership->get_id() !== $payment->get_membership_id()) {
return;
}

$payment->add_line_item(
new \WP_Ultimo\Checkout\Line_Item([
'hash' => 'test_metered_overage',
'type' => 'fee',
'title' => 'Metered overage',
'quantity' => 2,
'unit_price' => 4.00,
'recurring' => false,
'discountable' => false,
])
);

$context = [$remove_non_recurring, $save];
};

add_action('wu_membership_new_payment_pre_totals', $callback, 10, 4);

$payment = wu_membership_create_new_payment($membership, false, true, false);

remove_action('wu_membership_new_payment_pre_totals', $callback, 10);

$this->assertNotWPError($payment);
$this->assertSame([true, false], $context);
$this->assertEqualsWithDelta(57.00, $payment->get_total(), 0.001);
$this->assertArrayHasKey('LN_FEE_test_metered_overage', $payment->get_line_items());
}

/**
* Test wu_membership_create_new_payment cancels existing pending payment.
*/
Expand Down
50 changes: 50 additions & 0 deletions tests/WP_Ultimo/Models/Membership_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,56 @@
$this->assertInstanceOf(\WP_Error::class, $result);
}

/**
* Test swap recognizes addon-registered plan product types.
*/
public function test_swap_replaces_plan_for_registered_custom_plan_type(): void {
$register_network_type = static function (array $types): array {
$types[] = 'network';

return $types;
};

add_filter('wu_plan_product_types', $register_network_type);

$network_product = new Product(
[
'name' => 'Network Plan',
'slug' => 'network-plan-' . wp_generate_password(6, false),
'description' => 'A custom plan type registered by an addon',
'pricing_type' => 'paid',
'amount' => 49.00,
'currency' => 'USD',
'duration' => 1,
'duration_unit' => 'month',
'type' => 'network',
'recurring' => true,
'active' => true,
]
);
$network_product->set_skip_validation(true);
$network_product->save();
Comment on lines +1094 to +1110

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Use wu_create_product() for the product fixture.

The test rules require wu_create_*() helpers for test data. Assert that the helper does not return WP_Error before creating the cart.

Proposed fix
-		$network_product = new Product(
-			[
+		$network_product = wu_create_product(
+			[
 				'name'          => 'Network Plan',
 				'slug'          => 'network-plan-' . wp_generate_password(6, false),
 				'description'   => 'A custom plan type registered by an addon',
@@
 				'active'        => true,
+				'skip_validation' => true,
 			]
 		);
-		$network_product->set_skip_validation(true);
-		$network_product->save();
+		$this->assertNotWPError($network_product);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$network_product = new Product(
[
'name' => 'Network Plan',
'slug' => 'network-plan-' . wp_generate_password(6, false),
'description' => 'A custom plan type registered by an addon',
'pricing_type' => 'paid',
'amount' => 49.00,
'currency' => 'USD',
'duration' => 1,
'duration_unit' => 'month',
'type' => 'network',
'recurring' => true,
'active' => true,
]
);
$network_product->set_skip_validation(true);
$network_product->save();
$network_product = wu_create_product(
[
'name' => 'Network Plan',
'slug' => 'network-plan-' . wp_generate_password(6, false),
'description' => 'A custom plan type registered by an addon',
'pricing_type' => 'paid',
'amount' => 49.00,
'currency' => 'USD',
'duration' => 1,
'duration_unit' => 'month',
'type' => 'network',
'recurring' => true,
'active' => true,
'skip_validation' => true,
]
);
$this->assertNotWPError($network_product);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/WP_Ultimo/Models/Membership_Test.php` around lines 1094 - 1110, Replace
the manual Product construction, skip-validation call, and save in the network
product fixture with the `wu_create_product()` helper, preserving the existing
network product attributes. Assert that the helper result is not a `WP_Error`
before using it to create the cart.

Source: Coding guidelines


$cart = new \WP_Ultimo\Checkout\Cart(
[
'cart_type' => 'upgrade',
'products' => [$network_product->get_id()],
'duration' => 1,
'duration_unit' => 'month',
'membership_id' => $this->membership->get_id(),
'currency' => 'USD',
]
);

$result = $this->membership->swap($cart);

remove_filter('wu_plan_product_types', $register_network_type);

$this->assertSame($this->membership, $result);
$this->assertSame($network_product->get_id(), $this->membership->get_plan_id());
$this->assertNotContains($network_product->get_id(), $this->membership->get_addon_ids());
}

/**
* Test get_scheduled_swap returns false when nothing scheduled.
*/
Expand Down Expand Up @@ -1401,7 +1451,7 @@
/**
* Test renew() DOES clear date_cancellation when reactivating a cancelled membership.
*
* renew() is called by reactivate(), and also directly by gateways via IPN/webhook.

Check warning on line 1454 in tests/WP_Ultimo/Models/Membership_Test.php

View workflow job for this annotation

GitHub Actions / Code Quality Checks

Doc comment long description must start with a capital letter
* It must clear the cancellation timestamp when the previous status was CANCELLED
* so that cancelled membership records are cleaned up in a single save.
*/
Expand Down
Loading