autoship_get_scheduled_order_data

(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. The function also includes the ` autoship_create_scheduled_order_next_occurrence` filter 

Example

/**
* Creates and Gathers the Scheduled order Data
* Uses the following functions
* @see autoship_group_order_items
* @see autoship_get_scheduled_order_customer_data
* @see 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_id , $creation_date = null ) {

  // Check if the creation_date was supplied and if it is in the right format.
  if ( ( null === $creation_date ) || ( DateTime::createFromFormat('Y-m-d H:i:s', $creation_date ) !== FALSE) ) {

    $creation_date = date( 'Y-m-d H:i:s', time() );

  }

	// Get the wc order
	$order = wc_get_order( $order_id );
	if ( ! $order ) {
		return array();
	}

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

	// Process order items and remove non-autoship items.
	$grouped_items = autoship_group_order_items( $order_items );

	// If empty this order does not have scheduled items
  if ( empty( $grouped_items ) ) {
		return array();
	}

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

  // Get / Create the Order QPilot customer id ( uses API )
  $scheduled_order_data["CustomerId"] = autoship_get_autoship_customer_id( $order->get_user_id(), 'autoship_get_scheduled_order_data'  );

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

  // Create QPilot client instance.
	$client = autoship_get_default_client();

  // Gather the remaining data for the order by frequency / frequency type and next occurrence.
  // Then upsert the order.
	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'];

    // Update the OriginalExternalId to be unique to this grouping.
    // Generate a new unique id based on frequency & frequency_type
    $new_sheduled_order['OriginalExternalId'] .= '-' . $new_sheduled_order['Frequency'] . '-' . $new_sheduled_order['FrequencyType'];

		$next_occurrence = ( ! empty( $frequency_group['next_occurrence'] ) ) ? date_i18n( 'c', $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 );

    // If the next_occurrence doesn't exist check if it was already calculated so
    // We avoid an unneeded API call.
		if ( empty( $next_occurrence ) && isset( $occurrences[ $frequency_group['frequency_type'] . '-'. $frequency_group['frequency']] ) ) {

      $new_sheduled_order["NextOccurrenceUtc"] = $occurrences[ $frequency_group['frequency_type'] . '-'. $frequency_group['frequency']];

    // Not yet calculated so get next occurrence from API
    } else if ( empty( $next_occurrence ) ){

    	try {
    		$new_sheduled_order["NextOccurrenceUtc"] = $client->get_next_occurrence_utc( $frequency_group['frequency_type'],$frequency_group['frequency'], $creation_date );
        $occurrences[ $frequency_group['frequency_type'] . '-'. $frequency_group['frequency']] = $new_sheduled_order["NextOccurrenceUtc"];
    	} catch ( Exception $e ) {
    		error_log( sprintf( 'Error getting next occurrence for order #%d: %s', $order_id, $e->getMessage() ) );
    		throw $e;
    	}

    }

		// Loop through items and add to schedule_fields['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_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;


}

Still need help? Contact Us Contact Us