Home > OS >  Error occurs when try to get woocommerce order details in a custom WP_Query
Error occurs when try to get woocommerce order details in a custom WP_Query

Time:07-18

I'm trying to get order details with WP_Query, this must be work with an ajax call for pagination without page reload. The problem is that I get an error when I try to put some variables, such as: $order_id = $order->get_id(); or $items = $order->get_items();.

The error in google console: POST https://mywebsite.com/wp-admin/admin-ajax.php 500

As @Sadoo suggested, if I put $order_id = get_the_ID(); instead of $order_id = $loop->post->ID; the error in google console disappears.

At this point I should be able to see the details of the orders such as the ID for example, but I still don't get any results. I think the problem is the query because in with var_dump($loop); i get null. Why is this happening ?

functions.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();
}

custom-template.php

<div >
  <div id="primary" >
    <div >
      <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 ></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

If instead of using a WP_Query I use a SQL Query everything works fine. However I am trying to use WP_Query as I am more familiar with it. Below is the working query that returns the results correctly.

//--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>';

}

CodePudding user response:

the_post() returns True when finished. as a result of calling setup_postdata(). go ahead and try $order_id = get_the_ID(); instead of $order_id = $loop->post->ID; and see if it works.

CodePudding user response:

After some testing I was able to find a solution. I haven't made many changes, added a few foreaches and was able to access order details without receiving errors. Below I leave the working code. It can come in handy to anyone who finds the same problem.

functions.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;
  $msg = '';
  if(isset($_POST['page'])) {
    $page = sanitize_text_field($_POST['page']);
    $cur_page = $page;
    $page -= 1;
    $per_page = 4;
    $previous_btn = true;
    $next_btn = true;
    $start = $page * $per_page;
    
    // Query Wordpress the posts
    $loop = new WP_Query( array(
      'post_type'         => 'shop_order',
      'post_status'       => 'wc-completed',
      'orderby'           => 'post_date',
      'order'             => 'DESC',
      'posts_per_page'    => $per_page,
      'offset'            => $start,
    ));
    
    // 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 = $loop->post->ID;
       
       // Get an instance of the WC_Order Object
       $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();

          // Get product image - https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/
          $product = $item->get_product();
          if( $product instanceof WC_Product ){
            $order_img = $product->get_image();
          }
          //Get product download button 
          $downloads = $order->get_downloadable_items();
          if(is_array($downloads)) {
            foreach($downloads as $product){
             $download_button = '<a href="'. $product['download_url'] .'" target="_blank">Download</a>';
            } 
          }

          echo '
            <table >
            <tr >
              <td >
               <span >Ordine</span>
               <span>#'. esc_attr($orders_id) .'</span>
              </td>

              <td >
               <span >Prodotto</span>
               <a href="'. wp_kses_post($view_order) .'">'. wp_kses_post($product_name) .'</a>
              </td>

              <td >
               <span >Data</span>
               <span>'. wp_kses_post($date_created) .'</span>
              </td>

              <td >
               <span >Prezzo</span>
               <span>'. wp_kses_post($order_total) .'</span>
              </td>

              <td >
               <span >Stato</span>
               <span >'. wp_kses_post($status) .'</span>
              </td>

              <td >
               <span >File</span>
               <a target=”_blank” href="'. esc_url($view_order) .'">Visualizza<i ></i></a>
              </td>
            </tr>    
            </table> 
          ';
        }  
      }
      wp_reset_postdata();
    }
        
    // 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     
    $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();
}

custom-template.php

<div >
  <div id="primary" >
    <div >
      <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('p');
              load_all_posts(page);
            });
          }); 
        </script>
        <div class = "pag_loading">
          <div class = "pagination_container">
            <div ></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
  • Related