Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 247
René Meye Avatar asked Jul 18 '16 19:07

René Meye


3 Answers

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:

  • Official WooCommerce Conditional Tags
  • Display My Account link in a template file

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

like image 106
LoicTheAztec Avatar answered Oct 20 '22 17:10

LoicTheAztec


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.

like image 35
Dan. Avatar answered Oct 20 '22 19:10

Dan.


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.

like image 3
jeh Avatar answered Oct 20 '22 18:10

jeh