autoship_group_order_items

(src > orders.php) This function organizes an WC Order into groups by Frequency, Frequency Type, and Next Occurrence. It also strips out any non-autoship products. Replaces the autoship_group_cart_items function.

Example

/**
* Groups and Orders the supplied order or cart items by Autoship Frequency,
* Autoship Type, and Autoship Next Occurence.
* Order items included in an upsert to QPilot should be grouped by Autoship
*
* @param array $order_items.  The order or cart items to sort.
* @return array               The original order items sorted by Autoship values.
*                             any non-autoship items are removed.
*/
function autoship_group_order_items( $order_items ) {

  $counter = 0;

  // Group frequencies
	$frequencies_hash = array();
	foreach ( $order_items as $item_key => $item ) {

		if ( !isset( $item['autoship_frequency_type'] ) || !isset( $item['autoship_frequency'] ) ||
          empty( $item['autoship_frequency_type'] ) || empty( $item['autoship_frequency'] ) )
       continue;

			$next_occurrence = ( ! empty( $item['autoship_next_occurrence'] ) ) ? $item['autoship_next_occurrence'] : '';
			$key = $item['autoship_frequency_type'] . ';' . $item['autoship_frequency'] . ';' . $next_occurrence;

  		$frequency_type = $item['autoship_frequency_type'];
  		$frequency = $item['autoship_frequency'];


    if ( isset( $frequencies_hash[ $key ] ) ){
      $frequencies_hash[ $key ][ 'items' ][$item_key] = $item;
    } else {

			$frequencies_hash[ $key ] = array(
  			'frequency_type' => $frequency_type,
  			'frequency' => $frequency,
  			'next_occurrence' => $next_occurrence,
  			'items' => array(
          $item_key => $item
        )
  		);
    }

	}

  ksort($frequencies_hash);
  return array_values($frequencies_hash);
}