autoship_create_scheduled_orders (function)

(src/orders.php) Used to create and send Scheduled Orders to QPilot.  

Code

/**
* Creates and Sends scheduled orders to QPilot
* WC Orders are broken down and grouped by Frequency Type + Frequency + Next Occurrence
* @uses autoship_get_scheduled_order_data()
* @uses QPilotClient::upsert_order()
*
* Developers can modify QPilot Orders using the {@see autoship_create_scheduled_order_data}
* filter
*
* @param WC_Order|int $order A WC Order object or order id.
* @param string       Optional. $creation_date The date the order should be
*                     created based on should be 'Y-m-d H:i:s' format.
*                     Default NULL.
* @return array|WP_Error An array of ids created or WP_Error on failure.
*/
function autoship_create_scheduled_orders( $order, $creation_date = null ) {

  if ( is_numeric( $order ) ){
    $order = wc_get_order( $order );
  }

  $order_id = $order->get_id();

  // Create the scheduled order data to upsert
  // Group the data for the order by frequency, frequency type, and next occurrence.
  $group_scheduled_order_data = autoship_get_scheduled_order_data ( $order, $creation_date );

  if ( !empty( $group_scheduled_order_data ) ){

    // Set the Expected Scheduled Order count
    autoship_set_scheduled_order_expected_count( $order, count( $group_scheduled_order_data ) );

    // Set the Source if not already set.
    autoship_flag_if_scheduled_order_related( $order, 'source' );

  }

  $scheduled_order_ids = array();
  $error = '';
  // Loop through the order groups and upsert each.
  foreach ( $group_scheduled_order_data as $scheduled_order_data ) {

    $scheduled_order_data = apply_filters( 'autoship_create_scheduled_order_data', $scheduled_order_data, $order_id );

    // Create the order in QPilot.
    $scheduled_order = autoship_create_scheduled_order( $scheduled_order_data['customerId'], $scheduled_order_data );

    if ( is_wp_error( $scheduled_order ) ){

      // Attach the orders key, save the error and add an error note.
      $note   = sprintf( __('Creating Scheduled Orders from Order #%d Failed.  Additional Details: %s'), $order_id, $scheduled_order->get_error_message() );
      $error  = new WP_Error( 'Create Scheduled Orders Failed', $note );
      autoship_set_order_created_scheduled_orders_key( $order_id, $scheduled_order_ids );
      autoship_set_order_created_scheduled_orders_error( $order_id, $error );

      $order->add_order_note( $note );

    } else {

      // Tag on the order ids to this order's metadata.
      $scheduled_order_ids[] = $scheduled_order->id;
      update_post_meta( $order_id, '_autoship_created_scheduled_order_id', $scheduled_order_ids, false );

    }

  }

  if ( !empty( $group_scheduled_order_data ) ){

    // Add any last IDs
    autoship_set_order_created_scheduled_orders_key( $order_id, $scheduled_order_ids );

    // Only if no errors then add success note
    if ( empty( $error ) ){
    $note = sprintf( __('Autoship Scheduled Order Created Successfully: %s.'), implode( ', ' , $scheduled_order_ids ) );
    autoship_set_order_created_scheduled_orders_error( $order_id );
    $order->add_order_note( $note );
    do_action( 'autoship_create_scheduled_orders_success', $scheduled_order_ids, $order_id ); }

  }

  if ( !empty( $error ) )
  do_action( 'autoship_create_scheduled_orders_failure', $scheduled_order_ids, $order_id, $error );

  return $scheduled_order_ids;

}