Fetch complete order from woocomerce store by date and time php code


<?php

/**
 * FetchOrdersByDate 
 * Fetches orders placed on woocommerce by date
 * output is in JSON format 
 */
 
function fetchOrdersByDate($date){
 
 $args = array(
    // Arguments for query.
 'post_type'   => 'shop_order',
 'post_status' => 'publish',
 'orderby' => 'modified',
 'meta_key' => '_customer_user',
    'date_query'  => array(
  array(
   'after'    => $date,
   'inclusive' => true,
  ),
 ),
 'posts_per_page' => -1
 );
 
// Custom query.
$query = new WP_Query($args);
$result = array();

if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();

 $order_id = $query->post->ID;  
   
 $orderObj = new WC_Order($order_id);
 $items = $orderObj->get_items();
 $user_id = $orderObj->get_user_id(); // get user id of the order 
 

 foreach ( $items as $item ) {
 
  $jsonformat = array(
   "order"=> array (
    "item_name"=>$item['name'],
    "product_id"=>$item['product_id'],
    "variation_id"=>$item['variation_id'],
    "type"=>$item['type'],
    "order_number"=> $orderObj->id,
    "created_at"=> $orderObj->order_date,
    "updated_at"=> $orderObj->modified_date,    
    "status"=> $orderObj->get_status(),
    "currency"=> $orderObj->get_order_currency(),
    "tax_class" =>$item['tax_class'],
    "line_subtotal_tax"=>$item['line_subtotal_tax'],
    "total"=> $orderObj->get_total(),
    "subtotal"=> $orderObj->get_subtotal(),
    "total_line_items_quantity"=> $item['qty'],
    "total_tax"=> $orderObj->get_tax_totals(),
    "total_shipping"=> $orderObj->get_total_shipping(),
    "cart_tax"=> $orderObj->get_cart_tax(),
    "shipping_tax"=> $orderObj->get_shipping_tax(),
    "shipping_methods"=> $orderObj->get_shipping_methods(),
    "payment_details"=>array(
      "method_id"=> $orderObj->payment_method,
      "method_title"=> $orderObj->payment_method_title,
      "paid"=> $orderObj->needs_payment()  
    ),
    "shipping_address"=>array(
      "first_name"=> get_user_meta( $user_id, 'shipping_first_name', true ),
      "last_name"=> get_user_meta( $user_id, 'shipping_last_name', true ),
      "company"=> get_user_meta( $user_id, 'shipping_company', true ),
      "address_1"=> get_user_meta( $user_id, 'shipping_address_1', true ),
      "address_2"=> get_user_meta( $user_id, 'shipping_address_2', true ),
      "city"=> get_user_meta( $user_id, 'shipping_city', true ),
      "state"=> get_user_meta( $user_id, 'shipping_state', true ),
      "postcode"=> get_user_meta( $user_id, 'shipping_postcode', true ),
      "country"=> get_user_meta( $user_id, 'shipping_country', true )
    ),
    "billing_address"=>array(
      "first_name" => get_user_meta( $user_id, 'billing_first_name', true ), 
      "last_name" => get_user_meta( $user_id, 'billing_last_name', true ),
      "company" => get_user_meta( $user_id, 'billing_company', true ), 
      "address_1" => get_user_meta( $user_id, 'billing_address_1', true ), 
      "address_2" => get_user_meta( $user_id, 'billing_address_2', true ), 
      "city"  => get_user_meta( $user_id, 'billing_city', true ),
      "state" => get_user_meta( $user_id, 'billing_state', true ), 
      "postcode" => get_user_meta( $user_id, 'billing_postcode', true ), 
      "country" => get_user_meta( $user_id, 'billing_country', true )
      )
    )
   );
  array_push($result,$jsonformat);
 }
// end of the foreach
 
endwhile; wp_reset_postdata(); endif;

if(sizeof($result)>0){
 
 return json_encode($result);
 
}else
{
 return;
}


}

?>
And $date should be in the format like $date=2013-02-28 23:59:59

Popular posts from this blog

What are sales territory in Netsuite?

How to get and set a sublist in netsuite suitlet suitescript 2.0