Skip to content

Commit 912d4dc

Browse files
committed
Add support for signup coupons during subscription switches
1 parent 1378351 commit 912d4dc

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

includes/class-wc-subscriptions-coupon.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,16 @@ public static function get_discount_amount_for_cart_item( $cart_item, $discount,
219219
}
220220
}
221221

222-
// Apply sign-up discounts. Exclude switch cart items because their initial amount is entirely sign-up fees but should be treated as initial amounts
223-
if ( ! $is_switch && WC_Subscriptions_Product::get_sign_up_fee( $cart_item['data'] ) > 0 ) {
222+
// Compute the sign-up fee. If it's a switch, we need to get the signup fee less
223+
// recurring payment upgrade/downgrade costs.
224+
if ( $is_switch ) {
225+
$sign_up_fee = (int) $cart_item['data']->get_meta( '_subscription_sign_up_fee_prorated' );
226+
} else {
227+
$sign_up_fee = WC_Subscriptions_Product::get_sign_up_fee( $cart_item['data'] );
228+
}
229+
230+
// Apply sign-up discounts
231+
if ( $sign_up_fee > 0 ) {
224232

225233
if ( 'sign_up_fee' == $coupon_type ) {
226234
$apply_initial_coupon = true;
@@ -236,15 +244,15 @@ public static function get_discount_amount_for_cart_item( $cart_item, $discount,
236244
$cart_item['data'],
237245
array(
238246
'qty' => 1,
239-
'price' => WC_Subscriptions_Product::get_sign_up_fee( $cart_item['data'] ),
247+
'price' => $sign_up_fee,
240248
)
241249
);
242250
} else {
243251
$signup_fee = wc_get_price_excluding_tax(
244252
$cart_item['data'],
245253
array(
246254
'qty' => 1,
247-
'price' => WC_Subscriptions_Product::get_sign_up_fee( $cart_item['data'] ),
255+
'price' => $sign_up_fee,
248256
)
249257
);
250258
}
@@ -253,7 +261,7 @@ public static function get_discount_amount_for_cart_item( $cart_item, $discount,
253261
if ( in_array( $coupon_type, array( 'sign_up_fee', 'sign_up_fee_percent' ) ) ) {
254262
$discounting_amount = $signup_fee;
255263
} else {
256-
$discounting_amount -= $signup_fee;
264+
$discounting_amount = max( 0, $discounting_amount - $signup_fee );
257265
}
258266
}
259267

0 commit comments

Comments
 (0)