分页在订单WooCommerce中不起作用



我更改了WooCommerce订单的页码,如下所示:

  • 之前:example.com/my-account/orders/2
  • 之后:example.com/my-account/orders/page/2

我为WooCommerce订单添加了分页

路径:/plugins/wooccommerce/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 ); ?>

我还为函数添加了以下代码:

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

问题:点击任何页码后,它将转到正确的地址,即:

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

但在所有页面中,它都会带来与第一页相同的结果

我在互联网上做了很多搜索,我尝试了所有的方法,但都不起作用,例如:

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

如果你想回答我的问题,谢谢

要解决您的问题,请使用WP_Query,因为这里似乎使用了自定义页面模板。

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

应该是

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

因为静态首页使用page而不是paged

使用解决方案更新2,已测试并正在工作

我直接写结果,所以如果你不喜欢,不要浪费时间实现代码。不完全是你想要的,但我最多能做的是:example.com/my-account/orders/page/?n=2

我看了一整天这个话题,寻找类似的问题,并研究了参考资料https://developer.wordpress.org/reference/functions/paginate_links/,但无法按照您的问题中所述使其发挥作用。取得类似的结果也符合我的利益,我想了解它是如何运作的。无论如何,我们都在等待一个对这个问题很了解的人,也许他能给出更好的答案。

至于之前的更新,我会把我一步一步写给你

1。转到/themes/theme-child/woocommerce/myaccount

2。如果你有order.php文件打开它,否则创建它。然后你会有/themes/theme-child/woocommerce/myaccount/orders.php

3。在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 ) : ?>
<?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。在functions.php文件中,删除了以下函数,我在上一次更新中添加了它,但由于您现在有了自定义查询,因此不需要下面的代码。

// 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。仍然在functions.php文件中,删除您以前所做的,您不需要它。

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

试着让我们知道它是否有效,希望这能有所帮助。

最新更新