Woocommerce - How to show Order details (my-accoun

2020-06-29 06:15发布

问题:

Right now, in woocommerce the shortcode [woocommerce_my_account] displays the entire "My Account" page with all the tabbed options.

I only want to show the order details (found under the Orders tab) on a separate page.

So, can I just use the woocommerce template orders.php (woocommerce/templates/myaccount/orders.php) and use it on a blank new page or is there a better way to go about it?

回答1:

Woocommerce has function woocommerce_account_orders() which includes Orders template. You can follow the below steps to display only orders on a separate page.

Step-1: Place below code in your theme's functions.php (Place in child theme if you have created).

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');

Step-2: Place shortcode [woocommerce_orders] in any pages in admin side and check on front end.

Above Code will display orders if customer is loggedin otherwise it will display woocommerce's login/registration page.

If you want to hide "Orders" tabbed option which is on "My Account" Page then go to Admin Side and then Woocommerce > Settings > Accounts (tab) where you can see "My account endpoints", empty "orders" field there. It will hide "Orders" from frontend "My Account" page.

Edit: Make sure that you check on frontend loggedin as "Customer" user and that customer user has atleast one order placed otherwise blank page will display there.