Home > Back-end >  pagination does not work in orders WooCommerce
pagination does not work in orders WooCommerce

Time:07-25

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 WooCommerce\Templates
 * @version 3.7.0
 */

defined( 'ABSPATH' ) || exit;

do_action( 'woocommerce_before_account_orders', $has_orders ); 

?>

<?php if ( $has_orders ) : ?>

    <table >
        <thead>
            <tr>
                <?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
                    <th ><span ><?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 >
                    <?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
                        <td  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'] ) . '" >' . 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 >
            <?php
                $args = array(
                    'total'    => $customer_orders->max_num_pages
                );
                echo paginate_links( $args );
            ?>
        </nav>
    <?php endif; ?>

<?php else : ?>
    <p ><?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

CodePudding user response:

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

CodePudding user response:

Update with solution, tested and working

I'll tell you step by step what I did

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 has changed the final part, I'll explain further down what I did.

<?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 WooCommerce\Templates
 * @version 3.7.0
 */

defined( 'ABSPATH' ) || exit;

do_action( 'woocommerce_before_account_orders', $has_orders ); 

?>

<?php if ( $has_orders ) : ?>

    <table >
        <thead>
            <tr>
                <?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
                    <th ><span ><?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 >
                    <?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
                        <td  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'] ) . '" >' . esc_html( $action['name'] ) . '</a>';
                                    }
                                }
                                ?>
                            <?php endif; ?>
                        </td>
                    <?php endforeach; ?>
                </tr>
                <?php
            }
            ?>
        </tbody>
    </table>

    <?php do_action( 'woocommerce_before_account_orders_pagination' ); ?>
        <nav >
         <?php
            $args = array(
               'base'          => esc_url( wc_get_endpoint_url( 'orders') ) . '%_%', // Base of the paginated url.
               'format'        => '%#%', // 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'       => $current_page, // 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 »'.
            ); 
            echo paginate_links($args);
         ?> 
        </nav>
    

<?php else : ?>
    <p ><?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 add the following function, it will serve to determine how many orders you want to display for each page

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

What have I changed?

I changed the final part of the code which was wrong, it didn't work because you were not wrong method. So I changed this

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

to this

nav >
         <?php
            $args = array(
               'base'          => esc_url( wc_get_endpoint_url( 'orders') ) . '%_%', // Base of the paginated url.
               'format'        => '%#%', // 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'       => $current_page, // 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 »'.
            ); 
            echo paginate_links($args);
         ?> 
        </nav>

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

  • Related