phpwordpresswoocommerceconditional-statementsaccount

Detect dashboard of WooCommerce "my account" pages


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?


Solution

  • Update: Detecting specifically the My account "Dashboard" page

    <?php
        global $wp;
        
        // If NOT My account dashboard page
        if( ! ( basename($wp->request) === 'my-account' && is_account_page() ) ) { 
            printf('<a href="%s">%s</a>', 
            get_permalink( get_option('woocommerce_myaccount_page_id')),
            __('Back to my Account Dashboard', 'woocommerce') );
    
        }
    ?>
    
    <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…