Last active
April 25, 2023 14:56
-
-
Save jrick1229/0eb33b194a3c3a698827d52c3fbc771d to your computer and use it in GitHub Desktop.
WooCommerce Subscriptions – reduce the sign-up fee by a set amount when a recurring amount discount is added to the cart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'woocommerce_cart_calculate_fees','wcs_sign_fee_discount_with_recurring', 10, 1 ); | |
function wcs_sign_fee_discount_with_recurring( $cart ) { | |
global $product; | |
// set 999 equal to the post ID of the subscription product | |
$product_id = '999'; | |
$product = wc_get_product( $product_id ); | |
// set 2EAPV4V3 equal to the recurring coupon | |
$coupon_code = '2EAPV4V3'; | |
// set 0.09 equal to the percentage discount; must be a positive number | |
$percentage_discount *= 0.09; | |
if( in_array( wc_format_coupon_code( $coupon_code ), $cart->get_applied_coupons() ) && empty( $cart->recurring_cart_key ) ) { | |
// adding the title of the 'fee', in this case a sign-up fee discount | |
$title = __('Sign-up fee discount', 'woocommerce'); | |
$cost = -($product->get_price() * $percentage_discount); | |
$cart->add_fee( $title, $cost, false ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment