When using wc_get_orders() to retrieve WooCommerce orders, you can implement pagination by using the paged and limit arguments.

Example: Paginated Order Retrieval

Add this code to your theme’s functions.php file or a custom plugin:

function get_paginated_orders( $page = 1, $per_page = 10 ) {
    $args = array(
        'status' => array( 'processing', 'completed' ), // Filter by status (optional)
        'paginate' => true, // Enables pagination
        'limit' => $per_page, // Number of orders per page
        'paged' => $page, // Current page number
    );

    $orders = wc_get_orders( $args ); // Get orders
    return $orders;
}

// Example Usage:
$current_page = isset($_GET['paged']) ? absint($_GET['paged']) : 1;
$per_page = 10; // Adjust per your needs

$orders = get_paginated_orders( $current_page, $per_page );

// Display orders
foreach ( $orders->orders as $order ) {
    echo 'Order #' . $order->get_id() . '<br>';
}

// Pagination links
$total_pages = $orders->max_num_pages;

if ( $total_pages > 1 ) {
    echo '<div class="pagination">';
    for ( $i = 1; $i <= $total_pages; $i++ ) {
        echo '<a href="?paged=' . $i . '">' . $i . '</a> ';
    }
    echo '</div>';
}

How It Works:

  • paginate => true ensures pagination is enabled.
  • paged => $page specifies the current page.
  • $orders->orders contains the paginated orders.
  • $orders->max_num_pages returns the total number of pages.

Pagination UI (Improved with WordPress Functions)

For better pagination, use WordPress’s built-in pagination:

echo paginate_links( array(
    'total'   => $orders->max_num_pages,
    'current' => $current_page,
    'format'  => '?paged=%#%',
) );

This will generate next/previous links automatically.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x