Skip to content

Commit

Permalink
Add support for signup coupons during subscription switches
Browse files Browse the repository at this point in the history
  • Loading branch information
annemirasol committed Dec 18, 2024
1 parent 1378351 commit 912d4dc
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions includes/class-wc-subscriptions-coupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,16 @@ public static function get_discount_amount_for_cart_item( $cart_item, $discount,
}
}

// Apply sign-up discounts. Exclude switch cart items because their initial amount is entirely sign-up fees but should be treated as initial amounts
if ( ! $is_switch && WC_Subscriptions_Product::get_sign_up_fee( $cart_item['data'] ) > 0 ) {
// Compute the sign-up fee. If it's a switch, we need to get the signup fee less
// recurring payment upgrade/downgrade costs.
if ( $is_switch ) {
$sign_up_fee = (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'] );
}

// Apply sign-up discounts
if ( $sign_up_fee > 0 ) {

if ( 'sign_up_fee' == $coupon_type ) {
$apply_initial_coupon = true;
Expand All @@ -236,15 +244,15 @@ public static function get_discount_amount_for_cart_item( $cart_item, $discount,
$cart_item['data'],
array(
'qty' => 1,
'price' => WC_Subscriptions_Product::get_sign_up_fee( $cart_item['data'] ),
'price' => $sign_up_fee,
)
);
} else {
$signup_fee = wc_get_price_excluding_tax(
$cart_item['data'],
array(
'qty' => 1,
'price' => WC_Subscriptions_Product::get_sign_up_fee( $cart_item['data'] ),
'price' => $sign_up_fee,
)
);
}
Expand All @@ -253,7 +261,7 @@ public static function get_discount_amount_for_cart_item( $cart_item, $discount,
if ( in_array( $coupon_type, array( 'sign_up_fee', 'sign_up_fee_percent' ) ) ) {
$discounting_amount = $signup_fee;
} else {
$discounting_amount -= $signup_fee;
$discounting_amount = max( 0, $discounting_amount - $signup_fee );
}
}

Expand Down

0 comments on commit 912d4dc

Please sign in to comment.