移除woocommerce_subscription_totals_table钩子指定函数并替换模板失败求助
Fixing the Failed
remove_action for WooCommerce Subscriptions Totals Table I’ve run into this exact issue before—let’s break down why your code isn’t working and how to fix it quickly.
Why Your remove_action Isn’t Taking Effect
The main problem here is two-fold:
- Wrong Class Reference: When you used
array( __CLASS__, 'get_subscription_totals_template' ),__CLASS__points to your plugin’s class, not theWCS_Template_Loaderclass from WooCommerce Subscriptions. The original hook uses the static method fromWCS_Template_Loader, so you need to target that specific class. - Timing Issue: Your
remove_actionis probably running before WooCommerce Subscriptions has had a chance to add the original hook. Hooks need to be removed after they’re added, but before they fire.
Step-by-Step Solution
Here’s how to properly replace the subscription totals table:
Target the Correct Class & Method
Instead of using__CLASS__, explicitly referenceWCS_Template_Loader(the class that adds the original template hook).Run Your Code at the Right Time
Use theplugins_loadedhook with a higher priority (like 20) to ensure WooCommerce Subscriptions has loaded and added its hook before you try to remove it.
Example Code for Your Plugin
// Hook into plugins_loaded with a higher priority to ensure WCS is loaded add_action( 'plugins_loaded', 'custom_replace_subscription_totals', 20 ); function custom_replace_subscription_totals() { // Remove the original WCS totals table hook remove_action( 'woocommerce_subscription_totals_table', array( 'WCS_Template_Loader', 'get_subscription_totals_template' ) ); // Add your custom template hook add_action( 'woocommerce_subscription_totals_table', 'custom_subscription_totals_template' ); } function custom_subscription_totals_template( $subscription ) { // Load your custom template file // Replace the path with your actual template location ob_start(); include( plugin_dir_path( __FILE__ ) . 'templates/my-custom-subscription-totals.php' ); echo ob_get_clean(); }
If You’re Using a Class-Based Plugin
If your plugin is structured with a class, adjust the code like this:
class My_Subscription_Customizations { public function __construct() { // Hook in after WCS loads add_action( 'plugins_loaded', array( $this, 'replace_totals_table' ), 20 ); } public function replace_totals_table() { // Remove original hook remove_action( 'woocommerce_subscription_totals_table', array( 'WCS_Template_Loader', 'get_subscription_totals_template' ) ); // Add custom hook add_action( 'woocommerce_subscription_totals_table', array( $this, 'load_custom_totals' ) ); } public function load_custom_totals( $subscription ) { // Load your template include( plugin_dir_path( __FILE__ ) . 'templates/custom-totals.php' ); } } // Initialize your class new My_Subscription_Customizations();
Quick Notes for Your Custom Template
- Make sure your template uses the
$subscriptionobject to access subscription data (like totals, cycle details, etc.) just like the original WCS template does. - Test by viewing an active subscription page to confirm your template loads correctly.
内容的提问来源于stack exchange,提问作者Abhishek
相关产品推荐
相关产品推荐

