
本文旨在解决在使用 WP_Query 在 WooCommerce 中获取订单详情时遇到的常见错误,并提供优化的代码示例。我们将重点关注如何正确获取订单 ID、订单项目,以及如何在 AJAX 调用中实现分页功能,避免常见的 500 错误,并确保数据正确显示。
在使用 WP_Query 查询 WooCommerce 订单时,需要注意几个关键点,以避免常见的错误。首先,要确保正确设置查询参数,特别是 post_type 和 post_status。其次,在循环中正确获取订单 ID 和订单对象。
示例代码:
以下代码展示了如何使用 WP_Query 获取已完成的 WooCommerce 订单,并获取订单 ID 和订单项目:
<?php
add_action( 'wp_ajax_demo_pagination_posts', 'demo_pagination_posts' );
add_action( 'wp_ajax_nopriv_demo_pagination_posts', 'demo_pagination_posts' );
function demo_pagination_posts() {
global $wpdb;
if ( isset( $_POST[ 'page' ] ) ) {
# 保证页码大于等于 1
$page = max( intval( $_POST[ 'page' ] ), 1 );
# 每页显示订单数量
$per_page = 4;
// 查询已完成的订单
$args = array(
'post_type' => 'shop_order',
'post_status' => 'wc-completed',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => $per_page,
'paged' => $page,
);
$loop = new WP_Query( $args );
$count = $loop->found_posts;
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) {
$loop->the_post();
// 获取订单 ID
$order_id = $loop->post->ID;
// 获取订单对象
$order = wc_get_order( $loop->post->ID );
$items = $order->get_items();
$orders_id = $order->get_id();
$status = wc_get_order_status_name( $order->get_status() );
$date_created = $order->get_date_created()->date( 'd/m/Y' );
$payment_method = $order->get_payment_method_title();
$order_total = $order->get_formatted_order_total();
foreach ( $items as $item ) {
$product_name = $item->get_name();
$view_order = $order->get_view_order_url();
$product = $item->get_product();
if ( $product instanceof WC_Product ) {
$order_img = $product->get_image();
$downloads = $order->get_downloadable_items();
$download_button = '';
if ( is_array( $downloads ) ) {
foreach ( $downloads as $product ) {
$download_button = '<a href="' . $product[ 'download_url' ] . '" target="_blank">Download</a>';
}
}
$msg = '
<table class="table_orders" border="2">
<tr class="table_row_items">
<td class="product_number">
<span class="mobile title">Ordine</span>
<span>#' . esc_attr( $orders_id ) . '</span>
</td>
<td class="product_name">
<span class="mobile title">Prodotto</span>
<a href="' . wp_kses_post( $view_order ) . '">' . wp_kses_post( $product_name ) . '</a>
</td>
<td class="product_data">
<span class="mobile title">Data</span>
<span>' . wp_kses_post( $date_created ) . '</span>
</td>
<td class="product_price">
<span class="mobile title">Prezzo</span>
<span>' . wp_kses_post( $order_total ) . '</span>
</td>
<td class="product_status">
<span class="mobile title">Stato</span>
<span class="label ' . $order->get_status() . '">' . wp_kses_post( $status ) . '</span>
</td>
<td class="product_action">
<span class="mobile title">File</span>
<a target=”_blank” href="' . esc_url( $view_order ) . '">Visualizza<i class="fa-duotone fa-eye"></i></a>
</td>
</tr>
</table>
';
}
}
}
}
// 分页逻辑
$pages_num = intval( ceil( $count / $per_page ) );
$is_last_page = $pages_num == $page;
$is_first_page = $pages_num === 1;
if ( $page >= 7 ) {
$start_loop = $page - 3;
if ( $pages_num > $page + 3 )
$end_loop = $page + 3;
else if ( $page <= $pages_num && $page > $pages_num - 6 ) {
$start_loop = $pages_num - 6;
$end_loop = $pages_num;
} else {
$end_loop = $pages_num;
}
} else {
$start_loop = 1;
if ( $pages_num > 7 )
$end_loop = 7;
else
$end_loop = $pages_num;
}
$pagination_html = "<div class='pagination-link'>";
$pagination_html .= "
<ul>";
if ( !$is_first_page ) {
$pagination_html .= "<li data-pagenum='" . ( $page - 1 ) . "' class='active'>Previous</li>";
} else {
$pagination_html .= "<li class='inactive'>Previous</li>";
}
for ( $i = $start_loop; $i <= $end_loop; $i++ ) {
if ( $page == $i )
$pagination_html .= "<li data-pagenum='$i' class = 'selected' >{$i}</li>";
else
$pagination_html .= "<li data-pagenum='$i' class='active'>{$i}</li>";
}
if ( !$is_last_page ) {
$pagination_html .= "<li data-pagenum='" . ( $page + 1 ) . "' class='active'>Next</li>";
} else {
$pagination_html .= "<li class='inactive'>Next</li>";
}
$pagination_html .= "
</ul>";
$pagination_html .= "</div>";
echo
'<div class = "pagination-content">' . $msg . '</div>' .
'<div class = "pagination-nav">' . $pagination_html . '</div>';
}
die();
}
?>关键点:
为了实现无需页面刷新的分页功能,需要使用 AJAX。以下代码展示了如何使用 AJAX 调用上述函数,并更新页面内容:
HTML 代码 (custom-template.php):
<div class="wrap">
<div id="primary" class="content-area">
<div class="col-md-12 content">
<div class="inner-box content no-right-margin darkviolet">
<script type="text/javascript">
jQuery(document).ready(function ($) {
// This is required for AJAX to work on our page
var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
function load_all_posts(page) {
var data = {
page: page,
action: "demo_pagination_posts"
};
// Send the data
$.post(ajaxurl, data, function (response) {
$(".pagination_container").html(response);
});
}
load_all_posts(1); // Load page 1 as the default
$(document).on('click', '.pagination-link ul li', function () {
var page = $(this).attr('data-pagenum');
load_all_posts(page);
});
});
</script>
<div class="pag_loading">
<div class="pagination_container">
<div class="post-content"></div>
</div>
</div>
</div>
</div>
</div>
</div>关键点:
本文提供了一个完整的解决方案,用于在使用 WP_Query 在 WooCommerce 中获取订单详情时遇到的问题。 通过遵循这些步骤,你可以避免常见的错误,并实现高效、可靠的分页功能。记住,调试是解决问题的关键。 始终检查你的代码,并使用调试工具来查找错误。 通过理解这些概念并应用最佳实践,你可以构建强大的 WooCommerce 解决方案,满足你的业务需求。
以上就是WooCommerce 自定义 WP_Query 中获取订单详情时出错的解决方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号