15

How can I detect if the "myaccount/my-account.php" template is used on the Dashboard.

Currently I use:

<?php
    global $wp;
    if ( !isset($wp->query_vars['page']) ) {
?>
    <a href="/mein-konto/">Back to my Account</a>
<?php } ?>

<div class="myaccount_content">
    <?php
        do_action( 'woocommerce_account_content' );
    ?>
</div>

But that feels kind of hacky. Isn't there something like a is_myaccount_dashboard() function?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
René Meye
  • 173
  • 1
  • 1
  • 6

6 Answers6

24

Update: Detecting specifically the My account "Dashboard" page

<?php
    global $wp;
    $request = explode( '/', $wp->request );

    // If NOT in My account dashboard page
    if( ! ( end($request) == 'my-account' && is_account_page() ) ){ 
?>
    <a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id')); ?>">Back to my Account Dashboard</a>
<?php 
    } 
?>

<div class="myaccount_content">
    <?php
        do_action( 'woocommerce_account_content' );
    ?>
</div>

Tested and works.


Original answer:

Yes of course there is is_account_page() native WooCommerce conditional that returns true on the customer’s account pages.

Here is an example using is_account_page() and is_user_logged_in(). To get the my account link url you can use: get_permalink( get_option('woocommerce_myaccount_page_id') ).

if ( !is_account_page() ) { // User is NOT on my account pages

    if ( is_user_logged_in() ) { // Logged in user

    // Link to "My Account pages dashboard". 
?>  
    <a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e('My Account', 'woocommerce'); ?>"><?php _e( 'My Account', 'woocommerce' ); ?></a>
<?php }
    else { // User is NOT logged in

    // Link to "Login / register page".
?>  
    <a href="<?php echo get_permalink( get_option('woocommerce_myaccount_page_id') ); ?>" title="<?php _e( 'Login / Register','woocommerce' ); ?>"><?php _e( 'Login / Register', 'woocommerce' ); ?></a>

<?php 
    } 
} 
?>

Reference:


After that you can Override WooCommerce Templates via a Theme using my account templates to fine tune even more WooCommerce behaviors…

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    `is_account_page()` can't be used to detect 'the dashboard' of the 'my account' pages - this function returns true for all of the 'my account' pages. – Dan. Dec 21 '17 at 12:15
  • @Dan. Yes I Know… *"How can I detect if the "myaccount/my-account.php""* – LoicTheAztec Dec 21 '17 at 12:39
  • your original answer did not solve the question (neither the one in the question title, nor the one within the question). You've now just posted something that does solve his question, but this obviously doesn't invalidate my earlier comment and downvote. Whoever downvoted my answer (not pointing fingers) obviously just had a bone to pick. My solution definitely solves the question lol – Dan. Dec 21 '17 at 17:24
  • @Dan. You are really very smart, intelligent and mature. Congratulations! – LoicTheAztec Dec 21 '17 at 18:00
  • I know I am. I'm structuring my argument pretty well I think. I'm pretty confident everything I said in my preceding comment is valid. Why don't you explain to me why your original answer solved the questions and maybe I can be persuaded that I'm wrong/got the wrong point of view. Regarding the down vote - I'm pretty sure an answer that doesn't answer the question is basis for a downvote. Most of all, I'm disappointed in myself that I'm allowing this to take up my time, when I'm not going to benefit from it. I'm only human I suppose. – Dan. Dec 21 '17 at 18:15
4

To detect the exact page you're in, within the My Account area, (to allow you to determine which template is being used), I don't think Woocommerce provides a way.

I think you'll have to get the current URL, with vanilla PHP, and compare it to the URL of the page that is set to be the Dashboard/My Account Home page.

e.g.

$current_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$dashboard_url = get_permalink( get_option('woocommerce_myaccount_page_id'));

if($dashboard_url == $current_url){
    // do your stuff here
}

Woocommerce's is_account_page() conditional function will return true for ALL the My Account sub pages, so can't be used to determine if you're specifically on the Dashboard page.

Dan.
  • 609
  • 1
  • 6
  • 17
3

I had the same question (years later, lol). For people looking at the answer and wondering why it isn't helpful there are endpoint detecting functions available in woocommerce that do exactly what you're looking for. You can read the list of functions available here.

This is taken directly from the woocommerce docs. I am just copying it just incase the link is broken in the future

is_account_page() => Returns true on the customer’s account pages.

is_wc_endpoint_url() => Returns true when viewing any WooCommerce endpoint

is_wc_endpoint_url( 'order-pay' ) => When the endpoint page for order pay is being displayed.

is_wc_endpoint_url( 'order-received' ) => When the endpoint page for order received is being displayed.

is_wc_endpoint_url( 'view-order' ) => When the endpoint page for view order is being displayed.

is_wc_endpoint_url( 'edit-account' ) => When the endpoint page for edit account is being displayed.

is_wc_endpoint_url( 'edit-address' ) => When the endpoint page for edit address is being displayed.

is_wc_endpoint_url( 'lost-password' ) => When the endpoint page for lost password is being displayed.

is_wc_endpoint_url( 'customer-logout' ) => When the endpoint page for customer logout is being displayed.

is_wc_endpoint_url( 'add-payment-method' ) => When the endpoint page for add payment method is being displayed.

jeh
  • 577
  • 6
  • 18
3

Actually I found out this condition that seems to work fine in order to detect the WC Dashboard page with native WC code only:

    if (is_user_logged_in() && is_account_page() && !is_wc_endpoint_url()) {
      echo 'WC Dashboard';
    } else {
      echo 'no WC Dashboard';
    }
Marco
  • 31
  • 1
  • This one worked perfectly for me. I don't like the idea of trying to check the actual URL of the page since that can change. This solution uses conditional functions in a way that makes sense. I'm using this in the `body_class` filter to add a class of woocommerce-account-dashboard for styling purposes. – Ashley Strout Jan 04 '21 at 18:17
  • This will interfere with other account pages that don't have a wc endpoint (e.g. some third-party plugins might add a sub page to 'my account' which will return true for is_account_page and true for !is_wc_endpoint_url() – Dan. Mar 20 '22 at 10:33
1

I also needed to identify the dashboard specifically and found this question but I didn't like any of the answers and WooCommerce still has no built-in tag to do this...

I have 2 issues with the answers, the first is using is_wc_endpoint_url() (not infallible) and the second is comparing URLs (personal taste I guess?)

  1. is_wc_endpoint_url() can return false for endpoints which are added by a theme or plugin so you can get a false negative. There's a filter so you can add your own but you can't trust that a plugin will do that.
  2. Comparing URLs feels hacky to me. You can probably get trustworthy results and it's pretty straightforward, so it's not necessarily a bad way to do it. Although I would forgo hardcoding parts of the URL(s) or at least allow for translating.

Now if you think about it, WooCommerce itself knows without fail when to load dashboard.php so I just took that code and refactored it to simply identify the dashboard:

function is_dashboard(){
    global $wp;

    if( ! empty( $wp->query_vars ) ){
        foreach ( $wp->query_vars as $key => $value ) {
            // Ignore pagename param.
            if ( 'pagename' === $key ) {
                continue;
            }

            if ( has_action( 'woocommerce_account_' . $key . '_endpoint' ) ) {
                return false;
            }
        }
    }

    return true;
}

For me, this is the most foolproof way to do it, although if done correctly, comparing URLs might be sufficient. You just can't trust is_wc_endpoint_url() for this issue.

Hope this helps anyone still looking for an is_dashboard() or is_account_page('dashboard')

  • Thank you for the function. I also noticed when adding custom endpoints it will be missed with the previous solutions. This works like a charm! – Missblues Apr 02 '23 at 09:50
0
<?php if(is_page("account") && !is_wc_endpoint_url()) { ?>

Assuming your account page is at /account/, this will detect your dashboard.

If you were to only do is_page("account"), the conditional would trigger for all account pages. However, because the dashboard isn't considered a WC endpoint like 'view-order' or 'last-password' is, this simple check will do the job.

Justin DoCanto
  • 1,537
  • 1
  • 11
  • 8