You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

移除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:

  1. Wrong Class Reference: When you used array( __CLASS__, 'get_subscription_totals_template' ), __CLASS__ points to your plugin’s class, not the WCS_Template_Loader class from WooCommerce Subscriptions. The original hook uses the static method from WCS_Template_Loader, so you need to target that specific class.
  2. Timing Issue: Your remove_action is 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:

  1. Target the Correct Class & Method
    Instead of using __CLASS__, explicitly reference WCS_Template_Loader (the class that adds the original template hook).

  2. Run Your Code at the Right Time
    Use the plugins_loaded hook 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 $subscription object 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:51:14