2
votes

I am trying to make it so that the url.com/my-account or the shortcode [woocommerce_my_account] displays the orders instead of the dashboard that displays "Hello User (not user)?".

The only thing I have is for after logging in which redirects to the orders instead of the dashboard, but I then going to the /my-account still displays the dashboard which I don't want.

The closest code I found that does what I want is...

function woocommerce_orders() {
    $user_id = get_current_user_id();
    if ($user_id == 0) {
         return do_shortcode('[woocommerce_my_account]'); 
    }else{
        ob_start();
        wc_get_template( 'myaccount/my-orders.php', array(
            'current_user'  => get_user_by( 'id', $user_id),
            'order_count'   => $order_count
         ) );
        return ob_get_clean();
    }

}
add_shortcode('woocommerce_orders', 'woocommerce_orders');

However, if there are no orders placed then it comes out blank(doesn't display the "No order has been made yet." with shop button) and the my account nav-sidebar doesn't show up. Would I have to make a custom page-template for this to add in the woocommerce account nav-sidebar?

Edit: If I use the orders.php instead of my-orders.php then I am able to get the "No order has been made yet." But still no sidebar-nav

4

4 Answers

5
votes

You could try the following code (that is not perfect as it removes the access to the dashboard):

add_action( 'woocommerce_account_content', 'remove_dashboard_account_default', 5 );
function remove_dashboard_account_default() {
    remove_action( 'woocommerce_account_content', 'woocommerce_account_content', 10 );
    add_action( 'woocommerce_account_content', 'custom_account_orders', 10 );
}


function custom_account_orders( $current_page ) {
    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' ) ) {
                do_action( 'woocommerce_account_' . $key . '_endpoint', $value );
                return;
            }
        }
    }

    $current_page    = empty( $current_page ) ? 1 : absint( $current_page );
    $customer_orders = wc_get_orders( apply_filters( 'woocommerce_my_account_my_orders_query', array(
        'customer' => get_current_user_id(),
        'page'     => $current_page,
        'paginate' => true,
    ) ) );

    wc_get_template(
        'myaccount/orders.php',
        array(
            'current_page'    => absint( $current_page ),
            'customer_orders' => $customer_orders,
            'has_orders'      => 0 < $customer_orders->total,
        )
    );
}

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

3
votes

There's a much easier way: simply catch WordPress's parse_request, check if the request is for my-account (or whatever your account page slug is) and perform a redirect:

function vnm_wc_redirect_account_dashboard( $wp ) {

    if ( !is_admin() ) {
        //  Uncomment the following line if you want to see what the current request is
        //die( $wp->request );

        //  The following will only match if it's the root Account page; all other endpoints will be left alone

        if ( $wp->request === 'my-account' ) {
            wp_redirect( site_url( '/my-account/orders/' ) );
            exit;
        }
    }
}

add_action( 'parse_request', 'vnm_wc_redirect_account_dashboard', 10, 1 );
0
votes

I used code of LoicTheAztec and another complementary snippet, which removes dashboard tab:

// Remove or rename my account page navigation links (removes downloads and dashboard).
add_filter ( 'woocommerce_account_menu_items', 'my_account_menu_order' );
function my_account_menu_order() {
    $menuOrder = array(
        'orders'             => __( 'Orders', 'woocommerce' ),
        // 'downloads'          => __( 'Download', 'woocommerce' ),
        'edit-address'       => __( 'Addresses', 'woocommerce' ),
        'edit-account'        => __( 'Account details', 'woocommerce' ),
        'customer-logout'    => __( 'Logout', 'woocommerce' ),
        // 'dashboard'          => __( 'Dashboard', 'woocommerce' )
    );
    return $menuOrder;
}

I also created a snippet which causes orders tab to be highlighted by default. It's done through adding active class and then CSS opacity: 1 highlights it. Script will show only in account section to avoid bloat where it isn't needed:

// Make orders link highlighted by default in my account section.
add_action('wp_footer', 'taisho_dashboard_orders_highlight');
function taisho_dashboard_orders_highlight() {

    if (!is_account_page()) return; // Account section only

    global $wp;
    $acc_url = get_permalink( get_option( 'woocommerce_myaccount_page_id' ));
    $my_acc =  rtrim( $acc_url , '/' );
    $my_acc = explode( '/', $my_acc );

    ?>
    <script type="text/javascript">
        var dashboard_active = <?php echo $wp->request === end($my_acc) ?>;
        jQuery(document).ready(function($) {
            $('.woocommerce-MyAccount-navigation-link--orders').toggleClass('is-active', dashboard_active);
        });
    </script>

    <?php   
}
0
votes

Using parse_request action:

add_action( 'parse_request', function ( $wp ) {
    // Prevent the redirection, in the case,
    // the user is not logged in (no login, no orders)
    if (!is_user_logged_in()) return false;

    if ( $wp->request === 'my-account' ) {
        wp_redirect( home_url( '/my-account/orders/' ) );
        exit;
    }
}, 10, 1 );