Home > OS >  Get latest purchased date for given product
Get latest purchased date for given product

Time:11-05

Iam trying build a query to get latest purchase date for given product id but for now still having errors.

        function check_date_purchased($product_id){ 
    $args = array(
        "posts_per_page" => -1,
        "product_id" => $product_id,
        "orderby" => 'date',
        "order" => 'DESC',
        "status" => "completed",
    );
    $query = new WC_Order_Query( $args );
    $orders = $query->get_orders();
    foreach( $orders as $order_id ) {
       $order = wc_get_order( $order_id );
       $items = $order->get_items();
       foreach ($items as $order_item){
          if ($product_id == $order_item->get_product_id()) return $order->order_date;
       }
    }
   return false;
}
echo check_date_purchased( 8070);

CodePudding user response:

  1. First, this WC_Order_Query class does not have a product_id argument. This means that the "product_id" => $product_id line in your $args array is not doing anything. To get orders containing a specific product, you need to iterate over each order and check if it contains the product, as you're already doing in your function.

  2. Second, The function returns the date of the first order it finds that contains the product, not necessarily the latest. So to get the latest date, you should store the date of each order that contains the product, and return the latest one.

function check_date_purchased($product_id){ 
   $args = array(
       "posts_per_page" => -1,
       "orderby" => 'date',
       "order" => 'DESC',
       "status" => "completed",
   );
   $query = new WC_Order_Query( $args );
   $orders = $query->get_orders();
   $latest_date = null;
   foreach( $orders as $order_id ) {
      $order = wc_get_order( $order_id );
      $items = $order->get_items();
      foreach ($items as $order_item){
         if ($product_id == $order_item->get_product_id()){
             if ($latest_date === null || $order->get_date_created() > $latest_date) {
                $latest_date = $order->get_date_created();
             }
         }
      }
   }
  return $latest_date;
}
echo check_date_purchased(8070);

In the above code, I initialize $latest_date as null before the loop. Then, for each order that contains the product, check if the order's date is later than $latest_date. If it is, update $latest_date with the order's date. After the loop, return $latest_date, which will be the date of the latest order that contains the product, or null if no such order was found.

This function uses the WC_Order_Query class to query all completed orders, ordered by date in descending order. It then iterates over each order and its items, checking if the product is in the order. If it is, it updates $latest_date if necessary. Finally, it returns the latest date found, or null if no date was found.

I'm not so sure if the modified code will work, But I hope you will have a hint to solve your problem.

CodePudding user response:

Sure, the problem is in your loop. You're returning the purchase date as soon as you find a match, but if you want to return the latest purchase date, you should iterate through the entire list and compare the purchase date so you can check which one is the latest. Here are the changes you can make to do this:

function check_date_purchased($product_id){ 
   $args = array(
       "posts_per_page" => -1,
       "orderby" => 'date',
       "order" => 'DESC',
       "status" => "completed",
   );
   $query = new WC_Order_Query( $args );
   $orders = $query->get_orders();
   $latestPurchaseDate = false; // set this default value before loops to compare dates
   foreach ($orders as $order_id) {
       $order = wc_get_order($order_id);
       $items = $order->get_items();
       foreach ($items as $order_item) {
           if ($product_id == $order_item->get_product_id()) {
               $order_date = $order->get_date_created();
               // Compare dates to check if later than current stored latest
               if (!$latestPurchaseDate || $order_date > $latestPurchaseDate) {
                   $latestPurchaseDate = $order_date;
               }
           }
       }
   }
   return $latestPurchaseDate;
}
echo check_date_purchased( 8070);
  • Related