From 9376cada705eb85ec574ac402c5605201c71d20b Mon Sep 17 00:00:00 2001 From: Anne Mirasol Date: Thu, 19 Dec 2024 11:35:25 -0600 Subject: [PATCH] Add logic for when there are no prorated fees --- includes/class-wc-subscriptions-coupon.php | 14 ++++++-- .../test-class-wc-subscriptions-coupon.php | 36 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-subscriptions-coupon.php b/includes/class-wc-subscriptions-coupon.php index 5ca410fba..afe792795 100644 --- a/includes/class-wc-subscriptions-coupon.php +++ b/includes/class-wc-subscriptions-coupon.php @@ -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'] ); } diff --git a/tests/unit/test-class-wc-subscriptions-coupon.php b/tests/unit/test-class-wc-subscriptions-coupon.php index 0165c813c..ac3ba7ea5 100644 --- a/tests/unit/test-class-wc-subscriptions-coupon.php +++ b/tests/unit/test-class-wc-subscriptions-coupon.php @@ -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( @@ -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( @@ -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 + ) + ); } @@ -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( @@ -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 + ) + ); } }