skip to Main Content

i changed the pagination of WooCommerce orders as follows :

  • before : example.com/my-account/orders/2
  • after : example.com/my-account/orders/page/2

i added pagination to WooCommerce orders

path : /plugins/woocommerce/templates/myaccount/orders.php

<?php
/**
 * Orders
 *
 * Shows orders on the account page.
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/myaccount/orders.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerceTemplates
 * @version 3.7.0
 */

defined( 'ABSPATH' ) || exit;

do_action( 'woocommerce_before_account_orders', $has_orders ); 

?>

<?php if ( $has_orders ) : ?>

    <table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">
        <thead>
            <tr>
                <?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
                    <th class="woocommerce-orders-table__header woocommerce-orders-table__header-<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
                <?php endforeach; ?>
            </tr>
        </thead>

        <tbody>
            <?php
            foreach ( $customer_orders->orders as $customer_order ) {
                $order      = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
                $item_count = $order->get_item_count() - $order->get_item_count_refunded();
                ?>
                <tr class="woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr( $order->get_status() ); ?> order">
                    <?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
                        <td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
                            <?php if ( has_action( 'woocommerce_my_account_my_orders_column_' . $column_id ) ) : ?>
                                <?php do_action( 'woocommerce_my_account_my_orders_column_' . $column_id, $order ); ?>

                            <?php elseif ( 'order-number' === $column_id ) : ?>
                                <a href="<?php echo esc_url( $order->get_view_order_url() ); ?>">
                                    <?php echo esc_html( $order->get_order_number() ); ?>
                                </a>

                            <?php elseif ( 'order-date' === $column_id ) : ?>
                                <time datetime="<?php echo esc_attr( $order->get_date_created()->date( 'c' ) ); ?>"><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time>

                            <?php elseif ( 'order-total' === $column_id ) : ?>
                                <?php echo $order->get_formatted_order_total(); ?>

                            <?php elseif ( 'order-status' === $column_id ) : ?>
                                <?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?>

                            <?php elseif ( 'order-actions' === $column_id ) : ?>
                                <?php
                                $actions = wc_get_account_orders_actions( $order );

                                if ( ! empty( $actions ) ) {
                                    foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
                                        echo '<a href="' . esc_url( $action['url'] ) . '" class="woocommerce-button button ' . sanitize_html_class( $key ) . '">' . esc_html( $action['name'] ) . '</a>';
                                    }
                                }
                                ?>
                            <?php endif; ?>
                        </td>
                    <?php endforeach; ?>
                </tr>
                <?php
            }
            ?>
        </tbody>
    </table>

    <?php do_action( 'woocommerce_before_account_orders_pagination' ); ?>

    <?php if ( 1 < $customer_orders->max_num_pages ) : ?>
        <nav class="woocommerce-pagination">
            <?php
                $args = array(
                    'total'    => $customer_orders->max_num_pages
                );
                echo paginate_links( $args );
            ?>
        </nav>
    <?php endif; ?>

<?php else : ?>
    <p class="woocommerce_message"><?php esc_html_e( 'No order has been made yet.', 'woocommerce' ); ?></p>
<?php endif; ?>

<?php do_action( 'woocommerce_after_account_orders', $has_orders ); ?>

i also added the following code to functions :

function woocommerce_my_account_my_orders_query1() {
    $current_page = get_query_var( 'paged' ) ? (int) get_query_var( 'paged' ) : 1;
    $customer_orders = array(
        'customer' => get_current_user_id(),
        'page'     => $current_page,
        'paginate' => true,
    );
    return $customer_orders;
} add_filter( 'woocommerce_my_account_my_orders_query', 'woocommerce_my_account_my_orders_query1', 10, 1 );

problem : after clicking on any of the page numbers, it will go to the correct address, that is:

  • example.com/my-account/orders/page/2
  • example.com/my-account/orders/page/3
  • example.com/my-account/orders/page/4

but in all pages it brings the same results as the first page

i did a lot of searching on the internet and i tried everything, but it didn’t work, for example :

https://wordpress.stackexchange.com/questions/185600/pagination-not-working

if you would like to answer my question, thanks

2

Answers


  1. To solve your issue, make use of WP_Query as it seems that you are using a custom page template here.

    'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
    

    should be

    'paged' => ( get_query_var('page') ? get_query_var('page') : 1),
    

    as static front pages use page and not paged

    Login or Signup to reply.
  2. Update 2 with solution, tested and working

    I write the result directly, so if you don’t like it, don’t waste time implementing the code. Not quite exactly what you were looking for, but the most I managed to do is this: example.com/my-account/orders/page/?n=2

    I looked at the topic all day, looked for similar questions and studied the reference https://developer.wordpress.org/reference/functions/paginate_links/, but couldn’t get it to work as described in your question. It is also in my interest to achieve a similar result, I want to understand how it works. In any case, we are waiting for someone who knows the subject well, maybe he will be able to give a better answer.

    As for the previous update, I will write you what I did step by step

    1. Go to /themes/theme-child/woocommerce/myaccount

    2. If you have the order.php file open it, otherwise create it. Then you will have /themes/theme-child/woocommerce/myaccount/orders.php

    3. Inside the orders.php file paste the following template, this time I added custom queries at the beginning and changed the final part slightly.

    <?php
    /**
     * Orders
     *
     * Shows orders on the account page.
     *
     * This template can be overridden by copying it to yourtheme/woocommerce/myaccount/orders.php.
     *
     * HOWEVER, on occasion WooCommerce will need to update template files and you
     * (the theme developer) will need to copy the new files to your theme to
     * maintain compatibility. We try to do this as little as possible, but it does
     * happen. When this occurs the version of the template file will be bumped and
     * the readme will list any important changes.
     *
     * @see https://docs.woocommerce.com/document/template-structure/
     * @package WooCommerceTemplates
     * @version 3.7.0
     */
    
    defined( 'ABSPATH' ) || exit;
    
    do_action( 'woocommerce_before_account_orders', $has_orders ); 
    
    ?>
    
    <?php if ( $has_orders ) : ?>
      <?php
      $paged = max( 1, (int) filter_input( INPUT_GET, 'n' ) );
      $customer_orders = wc_get_orders( apply_filters('woocommerce_my_account_my_orders_query',array(
        'customer_id'     => get_current_user_id(),
        'posts_per_page' => 3, // post per page
        'paged'          => $paged,
        'paginate'       => true,
      )));
      ?>
    
      <table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">
        <thead>
          <tr>
            <?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
              <th class="woocommerce-orders-table__header woocommerce-orders-table__header-<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
            <?php endforeach; ?>
          </tr>
        </thead>
    
        <tbody>
          <?php
            foreach ( $customer_orders->orders as $customer_order ) {
              $order      = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
              $item_count = $order->get_item_count() - $order->get_item_count_refunded();
              ?>
                <tr class="woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr( $order->get_status() ); ?> order">
                  <?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
                    <td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
                      <?php if ( has_action( 'woocommerce_my_account_my_orders_column_' . $column_id ) ) : ?>
                        <?php do_action( 'woocommerce_my_account_my_orders_column_' . $column_id, $order ); ?>
    
                        <?php elseif ( 'order-number' === $column_id ) : ?>
                        <a href="<?php echo esc_url( $order->get_view_order_url() ); ?>">
                          <?php echo esc_html( $order->get_order_number() ); ?>
                        </a>
    
                        <?php elseif ( 'order-date' === $column_id ) : ?>
                        <time datetime="<?php echo esc_attr( $order->get_date_created()->date( 'c' ) ); ?>"><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time>
    
                        <?php elseif ( 'order-total' === $column_id ) : ?>
                        <?php echo $order->get_formatted_order_total(); ?>
    
                        <?php elseif ( 'order-status' === $column_id ) : ?>
                        <?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?>
    
                        <?php elseif ( 'order-actions' === $column_id ) : ?>
                        <?php
                          $actions = wc_get_account_orders_actions( $order );
                          if ( ! empty( $actions ) ) {
                            foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
                              echo '<a href="' . esc_url( $action['url'] ) . '" class="woocommerce-button button ' . sanitize_html_class( $key ) . '">' . esc_html( $action['name'] ) . '</a>';
                            }
                          }
                        ?>
                      <?php endif; ?>
                    </td>
                  <?php endforeach; ?>
                </tr>
              <?php
            }
          ?>
        </tbody>
      </table>
    
      <?php do_action( 'woocommerce_before_account_orders_pagination' ); ?>
      <nav class="woocommerce-pagination">
        <?php
          echo paginate_links(array(
            'base'          => esc_url( wc_get_endpoint_url( 'orders') ) . 'page/%_%', // Base of the paginated url.
            'format'        => '?n=%#%', // Format for the pagination structure.
            'total'         => $customer_orders->max_num_pages, // The total amount of pages. Default is the value WP_Query's max_num_pages or 1.
            'current'       => $paged, // The current page number. Default is 'paged' query var or 1.
            'show_all'      => false, // Whether to show all pages. Default false.
            'end_size'      => 3, // How many numbers on either the start and the end list edges. Default 1.
            'mid_size'      => 3, // How many numbers to either side of the current pages. Default 2.
            'prev_next'     => true, // Whether to include the previous and next links in the list. Default true.
            'prev_text'      => __('<span>« Prev</span>'), // The previous page text. Default '« Previous'.
            'next_text'     => __('<span>Next »</span>'), // The next page text. Default 'Next »'.
          )); 
        ?> 
      </nav> 
    
      <?php else : ?>
      <p class="woocommerce_message"><?php esc_html_e( 'No order has been made yet.', 'woocommerce' ); ?></p>
    <?php endif; ?>
    
    <?php do_action( 'woocommerce_after_account_orders', $has_orders ); ?>
    

    4. In the functions.php file remove the following function, I had it added to you in the previous update, but since you now have a custom query you don’t need the code below.

    // Query Post - How many orders do you want to display per page in the orders.php template ?
    add_filter( 'woocommerce_my_account_my_orders_query', 'custom_my_account_orders', 10, 1 );
    function custom_my_account_orders( $args ) {
       // Set the post per page
       $args['limit'] = 6;
       return $args;
    }
    

    5. Still in the functions.php file, delete what you did previously, you don’t need it.

    function woocommerce_my_account_my_orders_query1() {
        $current_page = get_query_var( 'paged' ) ? (int) get_query_var( 'paged' ) : 1;
        $customer_orders = array(
            'customer' => get_current_user_id(),
            'page'     => $current_page,
            'paginate' => true,
        );
        return $customer_orders;
    } add_filter( 'woocommerce_my_account_my_orders_query', 'woocommerce_my_account_my_orders_query1', 10, 1 );
    

    Try and let us know if it works, hope this helps.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search