autoship_get_scheduled_order_data (function)

(src > orders.php) This is the main data gather function which collects all the data needed for the upsert of the order. The function takes the WC Order object or WC Order ID and an optional creation date in 'Y-m-d H:i:s' format. The creation date allows the function to be called outside of the checkout process so orders can be manually generated and upserted. 

Function

/**
 * Creates and Gathers the Scheduled order Data
 * Uses the following functions
 * @uses autoship_group_order_items()
 * @uses autoship_get_scheduled_order_customer_data()
 * @uses autoship_get_scheduled_order_product_data()
 *
 * Developers can modify the next_occurrence value for each order using
 * the new {@see autoship_create_scheduled_order_next_occurrence} 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       An array containing the Autoship Scheduled
 *                     order data for the supplied WC Order
 */
function autoship_get_scheduled_order_data ( $order , $creation_date = null ) {

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

	if ( ! $order )
	return array();

  // Get the creation date either from "now" or the supplied date
  $creation_date = autoship_get_api_formatted_date( $creation_date, 'Y-m-d H:i:s' );

  // Get order items from the WC Order
  $order_items = $order->get_items();

	// Process order items and remove non-autoship items.
	// If empty this order does not have scheduled items
	if ( empty( $grouped_items = autoship_group_order_items( $order_items, $order ) ) )
	return array();

  // Get Global Schedule Order Customer Data for the order.
  $scheduled_order_data = autoship_get_scheduled_order_customer_data( $order );

  // Get the Scheduled Order Payment Data for the order
  $scheduled_order_data["paymentMethod"] = autoship_get_scheduled_order_payment_data( $order );

  // Get the Scheduled Order Status
  $scheduled_order_data["status"] = autoship_get_scheduled_order_default_status( $order );

  // Add the Origin to the Order data - 'CustomerCheckout' if created via checkout else 'CustomerApi'
  $scheduled_order_data["origin"] = 'checkout' == $order->get_created_via() ? 'CustomerCheckout' : 'CustomerApi';

  // Init a Container for the Occurrence dates.
  $occurrences = $group_scheduled_order_data = array();

  // Gather the remaining data for the order by frequency / frequency type and next occurrence.
	foreach ( $grouped_items as $frequency_group ) {

    // Grab the global data
    $new_sheduled_order = $scheduled_order_data;

    // Reset the type and frequency.
    $new_sheduled_order['frequencyType'] = $frequency_group['frequency_type'];
    $new_sheduled_order['frequency']     = $frequency_group['frequency'];

		$next_occurrence = !empty( $frequency_group['next_occurrence'] ) ?
    autoship_format_next_occurrence_for_save( $frequency_group['next_occurrence'] ) : null;

    $next_occurrence = apply_filters('autoship_create_scheduled_order_next_occurrence', $next_occurrence, $frequency_group['frequency_type'], $frequency_group['frequency'], $creation_date, $order );

    // Set the unique frequency key for use in the External ID and in the NextOccurrenceUtc Array
    $frequency_key = $new_sheduled_order['frequency'] . '-' . $new_sheduled_order['frequencyType'];

    // Get the originalExternalId to be unique to this grouping.
    // Generate a new unique id based on frequency & frequency_type & next occurrence
    $new_sheduled_order['originalExternalId'] = autoship_get_scheduled_order_external_id( $order, !empty( $next_occurrence ) ?
    "-{$frequency_key}-{$next_occurrence}" : "-{$frequency_key}" );

    /**
     * Gather and Assign the Next Occurrence Dates for each frequency and frequency Type.
     * 1. If it's not set but already set in the array for the frequency and frequency Type use it.
     * 2. If it's set use it and add to array
     * 3. Since it's not yet calculated let QPilot deal with it on upsert.
     */
    $new_sheduled_order["nextOccurrenceUtc"] = NULL;
    if ( empty( $next_occurrence ) && isset( $occurrences[$frequency_key] ) ) {

      // get it from the array since it exists.
      $new_sheduled_order["nextOccurrenceUtc"] = $occurrences[$frequency_key];

    } else if ( !empty( $next_occurrence ) ){

      // get the manually assigned next occurrence and add to array.
      $new_sheduled_order["nextOccurrenceUtc"] = $next_occurrence;
      $occurrences[$frequency_key] = $new_sheduled_order["nextOccurrenceUtc"];

    }

    $new_sheduled_order["utcOffset"] = apply_filters('autoship_create_scheduled_order_next_occurrence_offset', autoship_get_local_timezone_offset(), $new_sheduled_order["nextOccurrenceUtc"], $frequency_group['frequency_type'], $frequency_group['frequency'], $creation_date, $order );

		// Loop through items and add to 'scheduleOrderItems
		foreach ( $frequency_group['items'] as $item ) {

			$external_id = ! empty( $item['variation_id'] ) ? $item['variation_id'] : $item['product_id'];

      // Get the Product specific line item data
      $scheduled_order_item_data = array();
      $scheduled_order_item_data = autoship_get_scheduled_order_product_data( $external_id, $frequency_group['frequency_type'], $frequency_group['frequency'] );

      // Tag on the order quantity.
      $scheduled_order_item_data["quantity"]          = (int) $item['qty'];
      $scheduled_order_item_data                      = apply_filters( 'autoship_get_scheduled_order_data_full_item_data', $scheduled_order_item_data, $order->get_id(), $item, $frequency_group['frequency_type'], $frequency_group['frequency'] );
			$new_sheduled_order["scheduledOrderItems"][]  = $scheduled_order_item_data;

		}

    $group_scheduled_order_data[] = $new_sheduled_order;

	}

  return $group_scheduled_order_data;

}