Created
July 24, 2025 05:24
-
-
Save shameemreza/6813e4f6bae3981b6674e9185bf4318c to your computer and use it in GitHub Desktop.
One-time admin-side script to fix incorrect tax calculations in WooCommerce Subscriptions. Visit each affected subscription in the admin panel to trigger the recalculation. Safe to remove after use.
This file contains hidden or 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
| /** | |
| * Simple one-time fix for WooCommerce Subscription tax calculation. | |
| * | |
| * This function automatically recalculates taxes for subscriptions when viewing them in admin. | |
| * Add this code to your theme's functions.php or a custom plugin, visit each affected | |
| * subscription once, then remove the code. | |
| * | |
| * @since 1.0.0 | |
| */ | |
| function hafwpv_fix_subscription_taxes() { | |
| // Only run in admin | |
| if ( ! is_admin() ) { | |
| return; | |
| } | |
| // Check if we're on a subscription edit page | |
| global $pagenow, $post_type; | |
| if ( 'post.php' !== $pagenow && 'admin.php' !== $pagenow ) { | |
| return; | |
| } | |
| // Get subscription ID from URL | |
| $subscription_id = 0; | |
| if ( isset( $_GET['post'] ) ) { | |
| $subscription_id = absint( $_GET['post'] ); | |
| } elseif ( isset( $_GET['id'] ) ) { | |
| $subscription_id = absint( $_GET['id'] ); | |
| } | |
| if ( ! $subscription_id ) { | |
| return; | |
| } | |
| // Check if this is a subscription | |
| $subscription = wcs_get_subscription( $subscription_id ); | |
| if ( ! $subscription ) { | |
| return; | |
| } | |
| // Log that we're fixing this subscription | |
| wc_get_logger()->info( | |
| sprintf( 'Fixing tax calculation for subscription #%d', $subscription_id ), | |
| array( 'source' => 'subscription-tax-fix' ) | |
| ); | |
| // Force recalculation of taxes for all line items | |
| foreach ( $subscription->get_items() as $item_id => $item ) { | |
| $product = $item->get_product(); | |
| if ( ! $product ) { | |
| continue; | |
| } | |
| // Get the correct tax class from the product | |
| $tax_class = $product->get_tax_class(); | |
| // Update the item's tax class | |
| $item->set_tax_class( $tax_class ); | |
| $item->save(); | |
| } | |
| // Recalculate taxes and totals | |
| $subscription->calculate_taxes(); | |
| $subscription->calculate_totals(); | |
| $subscription->save(); | |
| // Add a note to the subscription | |
| $subscription->add_order_note( esc_html__( 'Tax calculation fixed by admin script.', 'hafwpv' ) ); | |
| // Log completion | |
| wc_get_logger()->info( | |
| sprintf( 'Tax calculation fixed for subscription #%d', $subscription_id ), | |
| array( 'source' => 'subscription-tax-fix' ) | |
| ); | |
| } | |
| add_action( 'admin_init', 'hafwpv_fix_subscription_taxes' ); |
Author
shameemreza
commented
Nov 4, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment