How to display WooCommerce My account dashboard using a shortcode

Viewed 705

I have a custom myaccount page and I want to display dashboard.php in it. How can we embed dashboard.php template (My account "dashboard") in a shortcode?

Based on How can we display the edit account form using shortcode? answer to my previous question, I tried the following in my functions.php:

//[account_dashboard]
add_shortcode('account_dashboard', 'display_account_dashboard');
function display_account_dashboard()
{
    return WC_Shortcode_My_Account::dashboard();
}

But it doesn't work

1 Answers

Updated: Added missing defined argument "current_user".

To get the template myaccount/dashboard.php embedded in a shortcode you will use the function wc_get_template() this way:

add_shortcode('account_dashboard', 'display_account_dashboard');
function display_account_dashboard()
{
    return wc_get_template(
        'myaccount/dashboard.php',
        array(
            'current_user' => get_user_by( 'id', get_current_user_id() )
        )
    );

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

USAGE: [account_dashboard]

Related