Mary X Nabors
Mary X Nabors

Reputation: 139

Display a customer's total order number

How do I display a customer's total order number on the woocommerce my account page?

enter image description here

Upvotes: 1

Views: 1533

Answers (1)

bhanu
bhanu

Reputation: 1860

You can use woocommerce_before_account_orders hook to display the number of orders.

function bks_action_woocommerce_before_account_orders( $has_orders ) {
    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => wc_get_order_types(),
        'post_status' => array_keys( wc_get_order_statuses() ), // Feel free to change the post_status to the once you want.
    ) );

    $total_orders = count($customer_orders);
    
    // If customer has any orders then only show total number of orders.
    if ( $has_orders ) {
        echo 'No. of order : ' . $total_orders;
    }

};
    
    // add the action
add_action( 'woocommerce_before_account_orders', 'bks_action_woocommerce_before_account_orders', 10, 1 );

Don't add this code in functions.php. Use a custom plugin so that your customizations aren't lost during updates. Use this : https://wordpress.org/plugins/code-snippets/

Tested and WORKS. Order page screenshot

Upvotes: 1

Related Questions