Estoy tratando de obtener detalles del pedido con WP_Query, esto debe funcionar con una llamada ajax para paginación sin recargar la página. El problema es que me da error cuando trato de poner algunas variables, como por ejemplo: $order_id = $order->get_id();
o $items = $order->get_items();
.
El error en la consola de Google: POST https://mywebsite.com/wp-admin/admin-ajax.php 500
Como sugirió @Sadoo, si pongo $order_id = get_the_ID();
en vez de $order_id = $loop->post->ID;
el error en la consola de google desaparece.
En este punto, debería poder ver los detalles de los pedidos, como la identificación, por ejemplo, pero aún no obtengo ningún resultado. Creo que el problema es la consulta porque con var_dump($loop); me sale nulo Por qué está pasando esto ?
funciones.php
<?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, $wp_query;
$msg = '';
if(isset($_POST['page'])){
// Sanitize the received page
$page = sanitize_text_field($_POST['page']);
$cur_page = $page;
$page -= 1;
$per_page = 2; //set the per page limit
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
// WP_Query Posts
$args = array(
'post_type' => 'shop_order',
'post_status ' => 'wc-completed',
'posts_per_page' => $per_page,
'offset' => $start
);
$loop = new WP_Query( $args );
// At the same time, count the number of queried posts
$count = new WP_Query(
array(
'post_type' => 'post',
'post_status ' => 'publish',
'posts_per_page' => -1
)
);
$count = $count->post_count;
// Loop through each order post object
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) {
$loop->the_post();
// The order ID
$order_id = get_the_ID();
// Get an instance of the WC_Order Object
$order = wc_get_order($loop->post->ID);
$orders_id = $order->get_id();
echo '<span>#'. esc_attr($orders_id) .'</span>';
}
}
// This is where the magic happens
$no_of_paginations = ceil($count / $per_page);
if ($cur_page >= 7) {
$start_loop = $cur_page - 3;
if ($no_of_paginations > $cur_page + 3)
$end_loop = $cur_page + 3;
else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) {
$start_loop = $no_of_paginations - 6;
$end_loop = $no_of_paginations;
} else {
$end_loop = $no_of_paginations;
}
} else {
$start_loop = 1;
if ($no_of_paginations > 7)
$end_loop = 7;
else
$end_loop = $no_of_paginations;
}
// Pagination Buttons logic
$pag_container .= "
<div class="pagination-link">
<ul>";
if ($previous_btn && $cur_page > 1) {
$pre = $cur_page - 1;
$pag_container .= "<li p='$pre' class="active">Previous</li>";
} else if ($previous_btn) {
$pag_container .= "<li class="inactive">Previous</li>";
}
for ($i = $start_loop; $i <= $end_loop; $i++) {
if ($cur_page == $i)
$pag_container .= "<li p='$i' class="selected" >{$i}</li>";
else
$pag_container .= "<li p='$i' class="active">{$i}</li>";
}
if ($next_btn && $cur_page < $no_of_paginations) {
$nex = $cur_page + 1;
$pag_container .= "<li p='$nex' class="active">Next</li>";
} else if ($next_btn) {
$pag_container .= "<li class="inactive">Next</li>";
}
$pag_container = $pag_container . "
</ul>
</div>";
echo
'<div class = "pagination-content">' . $msg . '</div>' .
'<div class = "pagination-nav">' . $pag_container . '</div>';
}
die();
}
plantilla-personalizada.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(paged){
var data = {
page: paged,
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 paged = $(this).attr('p');
load_all_posts(paged);
});
});
</script>
<div class = "pag_loading">
<div class = "pagination_container">
<div class="post-content"></div>
</div>
</div>
</div>
</div>
</div>
</div>
Si en lugar de usar un WP_Query uso una consulta SQL, todo funciona bien. Sin embargo, estoy tratando de usar WP_Query porque estoy más familiarizado con él. A continuación se muestra la consulta de trabajo que devuelve los resultados correctamente.
//--SQL Query--//
// Set the table where we will be querying data
$table_name = $wpdb->prefix . "posts";
// Query the posts
$loop = $wpdb->get_results($wpdb->prepare("
SELECT * FROM " . $table_name . " WHERE post_type="shop_order" AND post_status="wc-completed" ORDER BY post_date DESC LIMIT %d, %d", $start, $per_page ) );
// At the same time, count the number of queried posts
$count = $wpdb->get_var($wpdb->prepare("
SELECT COUNT(ID) FROM " . $table_name . " WHERE post_type="post" AND post_status="publish"", array() ) );
// Loop through each order post object
foreach( $loop as $customer_order ){
$order_id = $customer_order->ID; // The Order ID
// Get an instance of the WC_Order Object
$order = wc_get_order( $customer_order->ID );
$orders_id = $order->get_id();
echo '<span>#'. esc_attr($orders_id) .'</span>';
}
.