Skip to content

Commit

Permalink
Add logic for downgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
annemirasol committed Dec 19, 2024
1 parent e0a4c78 commit 7e79332
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
10 changes: 7 additions & 3 deletions includes/class-wc-subscriptions-coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,14 @@ public static function get_discount_amount_for_cart_item( $cart_item, $discount,
}
}

// Compute the sign-up fee. If it's a switch, we need to get the signup fee less
// recurring payment upgrade/downgrade costs.
// Compute the true sign-up fee. If it's a subscription upgrade, we need to get the fee
// before extra charges, e.g. prorated recurring payment, were applied.
if ( $is_switch ) {
$sign_up_fee = (int) $cart_item['data']->get_meta( '_subscription_sign_up_fee_prorated' );
$is_downgrade = isset( $cart_item['subscription_switch']['upgraded_or_downgraded'] ) &&
'downgraded' === $cart_item['subscription_switch']['upgraded_or_downgraded'];
$sign_up_fee = $is_downgrade ?
WC_Subscriptions_Product::get_sign_up_fee( $cart_item['data'] ) :
(int) $cart_item['data']->get_meta( '_subscription_sign_up_fee_prorated' );
} else {
$sign_up_fee = WC_Subscriptions_Product::get_sign_up_fee( $cart_item['data'] );
}
Expand Down
22 changes: 20 additions & 2 deletions tests/unit/test-class-wc-subscriptions-coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public function test_get_discount_amount_for_cart_item_sign_up_fee_coupons() {
'data' => $this->variable_subscription_product,
'quantity' => 1,
'subscription_switch' => [
'subscription_id' => 123,
'subscription_id' => 123,
'upgraded_or_downgraded' => 'upgraded',
],
);
$this->cart->empty_cart();
Expand Down Expand Up @@ -174,6 +175,22 @@ public function test_get_discount_amount_for_cart_item_sign_up_fee_coupons() {
$coupon_sign_up_fee_large
)
);

// Subscription switch -- downgrade
$cart_item['data']->update_meta_data( '_subscription_sign_up_fee', 10 );
$cart_item['data']->update_meta_data( '_subscription_sign_up_fee_prorated', 0 );
$cart_item['data']->update_meta_data( '_subscription_price_prorated', 0 );
$cart_item['subscription_switch']['upgraded_or_downgraded'] = 'downgraded';
$this->assertEquals(
1,
WC_Subscriptions_Coupon::get_discount_amount_for_cart_item(
$cart_item,
$discount,
$discounting_amount,
$single,
$coupon_sign_up_fee_percent
)
);
}


Expand Down Expand Up @@ -248,7 +265,8 @@ public function test_get_discount_amount_for_cart_item_recurring_fee_coupons() {
'data' => $this->variable_subscription_product,
'quantity' => 1,
'subscription_switch' => [
'subscription_id' => 123,
'subscription_id' => 123,
'upgraded_or_downgraded' => 'upgraded',
],
);
$this->cart->empty_cart();
Expand Down

0 comments on commit 7e79332

Please sign in to comment.