How to add new columns with meta data to WooCommerce subscriptions table on my-account/subscriptions/ page

145 Views Asked by At

I'm trying to add new columns to WooCommerce subscriptions table displayed to users on ..my-account/subscriptions/ page. I was able to do it for the orders page ..my-account/orders/ however for some reason the code does not work for the subscriptions page. I think the filter I'm calling is wrong.

my code looks like this:

function add_column_to_woo_subscriptions_table (){
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) && 
     class_exists( 'WC_Subscriptions' ) ) {

    // Hook to add new columns to the subscriptions table
    add_filter( 'wcs_account_subscriptions_columns', 'custom_wcs_account_subscriptions_columns'  );

    // Function to add new columns
    function custom_wcs_account_subscriptions_columns( $columns ) {
        // Add new columns at the beginning
    
    $new_columns = array(
            'subscription_name'    => __( 'Name', 'woocommerce-subscriptions' ),
            'subscription_address' => __( 'Address', 'woocommerce-subscriptions' )
        );

        // Merge new columns with existing ones
        return array_merge( $new_columns, $columns );
    }

    // Hook to output data in the new columns
    add_action( 'woocommerce_my_account_my_subscriptions_column_subscription_name', 'custom_wcs_account_my_subscriptions_column_subscription_name' );
    add_action( 'woocommerce_my_account_my_subscriptions_column_subscription_address', 'custom_wcs_account_my_subscriptions_column_subscription_address' );

    // Function to output data for 'Name' column
    function custom_wcs_account_my_subscriptions_column_subscription_name( $subscription ) {
        // CALL to meta_data_1
    }

    // Function to output data for 'Address' column
    function custom_wcs_account_my_subscriptions_column_subscription_address( $subscription ) {
        // CALL to meta_data_2
    }
    
}
}

I'm expecting to see the new columns with the values of the meta data in the subscriptions table on the ..my-account/subscriptions/ page.

The only solution I found is using a child theme template: WordPress WooCommerce add new field in each row at /my-account/subscriptions/ page

If possible, I rather solve this with custom function in my custom plugin...

Any help would be appreciated. Thanks,

1

There are 1 best solutions below

1
On

to resolve this issue I created a custom template in my plugin and used the wc_get_template filter to override the WC my-subscriptions.php template.