请帮助我在我的订单历史页面中添加分页。分页应该像WooCommerce产品页面一样工作。
这是我的代码:
<?php $customer_orders = get_posts( apply_filters( \'woocommerce_my_account_my_orders_query\', array(
\'numberposts\' => \'20\',
\'meta_key\' => \'_customer_user\',
\'meta_value\' => get_current_user_id(),
\'post_type\' => wc_get_order_types( \'view-orders\' ),
\'post_status\' => array_keys( wc_get_order_statuses() )
) ) );
if ( $customer_orders ) : ?>
<h2><?php echo apply_filters( \'woocommerce_my_account_my_orders_title\', __( \'Order History\', \'woocommerce\' ) ); ?></h2>
<table class="shop_table shop_table_responsive my_account_orders">
<thead>
<tr>
<th class="order-number"><span class="nobr"><?php _e( \'Order\', \'woocommerce\' ); ?></span></th>
<th class="order-date"><span class="nobr"><?php _e( \'Date\', \'woocommerce\' ); ?></span></th>
<th class="order-status"><span class="nobr"><?php _e( \'Status\', \'woocommerce\' ); ?></span></th>
<th class="order-total"><span class="nobr"><?php _e( \'Total\', \'woocommerce\' ); ?></span></th>
<th class="order-actions"> </th>
</tr>
</thead>
<tbody><?php
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order();
$order->populate( $customer_order );
$item_count = $order->get_item_count();
?><tr class="order">
<td class="order-number" data-title="<?php _e( \'Order Number\', \'woocommerce\' ); ?>">
<a href="<?php echo $order->get_view_order_url(); ?>">
#<?php echo $order->get_order_number(); ?>
</a>
</td>
<td class="order-date" data-title="<?php _e( \'Date\', \'woocommerce\' ); ?>">
<time datetime="<?php echo date( \'Y-m-d\', strtotime( $order->order_date ) ); ?>" title="<?php echo esc_attr( strtotime( $order->order_date ) ); ?>"><?php echo date_i18n( get_option( \'date_format\' ), strtotime( $order->order_date ) ); ?></time>
</td>
<td class="order-status" data-title="<?php _e( \'Status\', \'woocommerce\' ); ?>" style="text-align:left; white-space:nowrap;">
<?php echo wc_get_order_status_name( $order->get_status() ); ?>
</td>
<td class="order-total" data-title="<?php _e( \'Total\', \'woocommerce\' ); ?>">
<?php echo sprintf( _n( \'%s for %s item\', \'%s for %s items\', $item_count, \'woocommerce\' ), $order->get_formatted_order_total(), $item_count ); ?>
</td>
<td class="order-actions">
<?php
$actions = array();
if ( in_array( $order->get_status(), apply_filters( \'woocommerce_valid_order_statuses_for_payment\', array( \'pending\', \'failed\' ), $order ) ) ) {
$actions[\'pay\'] = array(
\'url\' => $order->get_checkout_payment_url(),
\'name\' => __( \'Pay\', \'woocommerce\' )
);
}
if ( in_array( $order->get_status(), apply_filters( \'woocommerce_valid_order_statuses_for_cancel\', array( \'pending\', \'failed\' ), $order ) ) ) {
$actions[\'cancel\'] = array(
\'url\' => $order->get_cancel_order_url( wc_get_page_permalink( \'myaccount\' ) ),
\'name\' => __( \'Cancel\', \'woocommerce\' )
);
}
$actions[\'view\'] = array(
\'url\' => $order->get_view_order_url(),
\'name\' => __( \'View\', \'woocommerce\' )
);
$actions = apply_filters( \'woocommerce_my_account_my_orders_actions\', $actions, $order );
if ($actions) {
foreach ( $actions as $key => $action ) {
echo \'<a href="\' . esc_url( $action[\'url\'] ) . \'" class="view_order_dtls button \' . sanitize_html_class( $key ) . \'">\' . esc_html( $action[\'name\'] ) . \'</a>\';
}
}
?>
</td>
</tr><?php
}
?></tbody>
</table>
<?php endif; ?>
SO网友:Khushbu Padalia
我在订单历史页面中添加了分页功能,它正在工作。更换回路上方的以下代码
$customer_orders1 = get_posts(apply_filters(\'woocommerce_my_account_my_orders_query\', array(
\'numberposts\' => -1,
\'meta_key\' => \'_customer_user\',
\'meta_value\' => get_current_user_id(),
\'post_type\' => wc_get_order_types(\'view-orders\'),
\'post_status\' => array_keys(wc_get_order_statuses())
)));
$total_records = count($customer_orders1);
$posts_per_page = 20;
$total_pages = ceil($total_records / $posts_per_page);
$paged = ( get_query_var(\'page\') ) ? get_query_var(\'page\') : 1;
$customer_orders = get_posts(array(
\'meta_key\' => \'_customer_user\',
\'meta_value\' => get_current_user_id(),
\'post_type\' => wc_get_order_types(\'view-orders\'),
\'posts_per_page\' => $posts_per_page,
\'paged\' => $paged,
\'post_status\' => array_keys(wc_get_order_statuses())
));
循环完成后添加分页
<div class="pagination">
<?php
$args = array(
\'base\' => \'%_%\',
\'format\' => \'?page=%#%\',
\'total\' => $total_pages,
\'current\' => $paged,
\'show_all\' => False,
\'end_size\' => 5,
\'mid_size\' => 5,
\'prev_next\' => True,
\'prev_text\' => __(\'« Previous\'),
\'next_text\' => __(\'Next »\'),
\'type\' => \'plain\',
\'add_args\' => False,
\'add_fragment\' => \'\'
);
echo paginate_links($args);
?>
</div>