Skip to content

Commit

Permalink
Add logic for when there are no prorated fees
Browse files Browse the repository at this point in the history
  • Loading branch information
annemirasol committed Dec 19, 2024
1 parent 58a1c7a commit 9376cad
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
14 changes: 11 additions & 3 deletions includes/class-wc-subscriptions-coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,18 @@ 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 original sign-up fee. If it's a switch, we need to get the signup fee less
// upgrade costs.
if ( $is_switch ) {
$sign_up_fee = (int) $cart_item['data']->get_meta( '_subscription_sign_up_fee_prorated' );
$sign_up_fee_prorated = (int) $cart_item['data']->get_meta( '_subscription_sign_up_fee_prorated' );
$price_prorated = (int) $cart_item['data']->get_meta( '_subscription_price_prorated' );

if ( 0 === $sign_up_fee_prorated && 0 === $price_prorated ) {
// No prorated recurring fees, i.e. no extra upgrade costs, so we can use the original sign-up fee.
$sign_up_fee = WC_Subscriptions_Product::get_sign_up_fee( $cart_item['data'] );
} else {
$sign_up_fee = $sign_up_fee_prorated;
}
} else {
$sign_up_fee = WC_Subscriptions_Product::get_sign_up_fee( $cart_item['data'] );
}
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/test-class-wc-subscriptions-coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function test_get_discount_amount_for_cart_item_sign_up_fee_coupons() {
$this->cart->add_to_cart( $cart_item['data']->get_id() );
$cart_item['data']->update_meta_data( '_subscription_sign_up_fee', 30 );
$cart_item['data']->update_meta_data( '_subscription_sign_up_fee_prorated', 10 );
$cart_item['data']->update_meta_data( '_subscription_price_prorated', 20 );
$this->assertEquals(
1,
WC_Subscriptions_Coupon::get_discount_amount_for_cart_item(
Expand Down Expand Up @@ -141,7 +142,9 @@ public function test_get_discount_amount_for_cart_item_sign_up_fee_coupons() {
);

// Subscription switch -- no sign up fee, no discount
$cart_item['data']->update_meta_data( '_subscription_sign_up_fee', 20 );
$cart_item['data']->update_meta_data( '_subscription_sign_up_fee_prorated', 0 );
$cart_item['data']->update_meta_data( '_subscription_price_prorated', 20 );
$this->assertEquals(
0,
WC_Subscriptions_Coupon::get_discount_amount_for_cart_item(
Expand Down Expand Up @@ -174,6 +177,22 @@ public function test_get_discount_amount_for_cart_item_sign_up_fee_coupons() {
$coupon_sign_up_fee_large
)
);

// Subscription switch -- no prorated fees, e.g. downgrade
$discounting_amount = 10;
$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 );
$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 @@ -255,6 +274,7 @@ public function test_get_discount_amount_for_cart_item_recurring_fee_coupons() {
$this->cart->add_to_cart( $cart_item['data']->get_id() );
$cart_item['data']->update_meta_data( '_subscription_sign_up_fee', 30 );
$cart_item['data']->update_meta_data( '_subscription_sign_up_fee_prorated', 10 );
$cart_item['data']->update_meta_data( '_subscription_price_prorated', 20 );
$this->assertEquals(
2,
WC_Subscriptions_Coupon::get_discount_amount_for_cart_item(
Expand All @@ -276,5 +296,21 @@ public function test_get_discount_amount_for_cart_item_recurring_fee_coupons() {
$coupon_recurring_fee
)
);

// Subscription switch -- no prorated fees, e.g. downgrade
$discounting_amount = 10;
$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 );
$this->assertEquals(
0,
WC_Subscriptions_Coupon::get_discount_amount_for_cart_item(
$cart_item,
$discount,
$discounting_amount,
$single,
$coupon_recurring_percent
)
);
}
}

0 comments on commit 9376cad

Please sign in to comment.