首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分页在orders WooCommerce中无效

分页在orders WooCommerce中无效
EN

Stack Overflow用户
提问于 2022-07-25 07:53:53
回答 2查看 257关注 0票数 1

我将WooCommerce订单的分页更改如下:

example.com/my-account/orders/2

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

我在WooCommerce订单中添加了分页

路径: /plugins/woocommerce/templates/myaccount/orders.php

代码语言:javascript
复制
<?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 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 ); ?>

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

代码语言:javascript
复制
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

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

EN

回答 2

Stack Overflow用户

发布于 2022-07-25 09:08:03

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

代码语言:javascript
复制
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),

应该是

代码语言:javascript
复制
'paged' => ( get_query_var('page') ? get_query_var('page') : 1),

作为静态头版,使用page而不是paged

票数 0
EN

Stack Overflow用户

发布于 2022-07-25 11:46:11

使用解决方案、测试和工作更新2

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

我一整天都在研究这个话题,寻找类似的问题,并研究了参考https://developer.wordpress.org/reference/functions/paginate_links/,但是无法像在您的问题中所描述的那样让它发挥作用。取得类似的结果也符合我的利益,我想了解它是如何工作的。在任何情况下,我们都在等待一个熟悉这个问题的人,也许他能给出一个更好的答案。

至于上一次更新,我会一步一步地写给你。

1。转到/主题/主题-儿童/woocommerce/我的帐户

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

3。在orders.php文件中粘贴以下模板,这一次我在开头添加了自定义查询,并稍微更改了最后一部分。

代码语言:javascript
复制
<?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 ) : ?>
  <?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文件中,删除以下函数,我在上一次更新中将其添加到您,但是由于您现在有了一个自定义查询,所以不需要下面的代码。

代码语言:javascript
复制
// 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文件中,删除以前所做的事情,您不需要它。

代码语言:javascript
复制
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 );

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73105735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档