autoship_search_all_scheduled_orders()

(src/orders.php) Searches all Scheduled Orders for a site, can be used to search one customer's Scheduled Orders or Scheduled Orders across all customers. It takes the following parameters:

  • $customer_id 
  • $params (optional)

The function returns an array of Scheduled Order objects if successful or a Wordpress error if it fails.

Function

/**
 * Searches for scheduled order(s) in QPilot
 * @uses QPilotClient::get_orders()
 * If Page is not supplied it's assumed retrieve all orders.
 *
 * @param int       $customer_id   Optional. An Autoship customer id.
 * @param array     $params {
 *     Optional. An array of search parameters.
 *
 *     @type int     $pageSize             The default page size.  Default 100
 *     @type string  $orderBy              A product property to sort the results by
 *     @type string  $order                The Sort Direction the results should be returned ( DESC vs ASC )
 *     @type array   $statusNames          Array of Status names to search for.
 *     @type array   $metadataKey	         Array of Order Metadata keys to search for.
 *     @type array   $metadataValue	       Array of Order Metadata Values to search for.
 *     @type string  $search               A query string to search for.
 * }
 * @return array|WP_Error          Array of Scheduled order objects
 *                                 WP_Error on failure.
 */
function autoship_search_all_scheduled_orders( $customer_id = NULL, $index = 1, $params = array() ){

  $params = wp_parse_args( $params, array( 'pageSize' => 100 ) );
  $params['page'] = $index;

  $client = autoship_get_default_client();

  try {

    $orders = $client->get_orders( $customer_id, $params );

  } catch ( Exception $e ) {

    $notice = autoship_expand_http_code( $e->getCode() );

    if ( '404' == $e->getCode() ){
      $orders = new WP_Error( 'Order(s) Not Found', __( "Orders could not be found in QPilot", "autoship" ) );
      $message = isset( $customer_id ) ?
      sprintf( '%d Order(s) Not Found for Customer %d. Additional Details: %s', $e->getCode(), $customer_id, $e->getMessage() ) : sprintf( '%d Order(s) Not Found matching the search criteria. Additional Details: %s', $e->getCode(), $e->getMessage() );
  		autoship_log_entry( __( 'Autoship Orders', 'autoship' ), $message );
    } else {
      $orders  = new WP_Error( 'Order Retrieval Failed', __( $notice['desc'], "autoship" ) );
      $message = isset( $customer_id ) ?
      sprintf( '%d Order Retrieval Failed for Customer %d. Additional Details: %s', $e->getCode(), $customer_id, $e->getMessage() ) : sprintf( '%d Order Retrieval Failed for the search criteria. Additional Details: %s', $e->getCode(), $e->getMessage() );
  		autoship_log_entry( __( 'Autoship Orders', 'autoship' ), $message );
    }

  }

  if ( is_wp_error( $orders ) )
  return $orders;

  if ( $orders->totalPages > $index ){

    $index++;
    $new_orders = autoship_search_all_scheduled_orders( $customer_id, $index, $params );
    return !is_wp_error( $new_orders ) ? array_merge( $orders->items, $new_orders ) : $new_orders;

  }

  return $orders->items;

}