Skip to content

Instantly share code, notes, and snippets.

@CrispDev
Created July 15, 2022 18:31
Show Gist options
  • Save CrispDev/be45a370bdc2504d721156cfe7a372a3 to your computer and use it in GitHub Desktop.
Save CrispDev/be45a370bdc2504d721156cfe7a372a3 to your computer and use it in GitHub Desktop.
Autoship Customization // Gather the Next Occurrence dates for all Scheduled orders created at checkout
/**
* Gather the Next Occurrence dates for all Scheduled orders created at checkout
* and save those dates to the original WC Checkout Order
*
* @param array $scheduled_order_ids The created Scheduled Order IDs
* @param int $order_id The WC Order ID
* @param array $scheduled_orders The created Scheduled Orders in stdClass objects
*/
function xx_track_scheduled_order_next_occurrences_at_checkout( $scheduled_order_ids, $order_id, $scheduled_orders ){
// Loop Through the Created SO stdClass objects
$next_occurrences = [];
foreach ( $scheduled_orders as $scheduled_order_id => $scheduled_order ) {
/**
* The nextOccurrenceUtc is in Y-m-d\TH:i:s.v\Z format
* Use the utoship_get_formatted_local_date() function to convert the datetime string to local timezone
* and display in wc_date_format() format
*/
$next_occurrences[] = $scheduled_order->nextOccurrenceUtc;
}
// Now do something with the collected next occurrences
// example: save them all to the original checkout order
update_post_meta( $order_id, '_autoship_created_scheduled_orders_next_occurrences', $next_occurrences, false );
}
add_action( 'autoship_create_scheduled_orders_success', 'xx_track_scheduled_order_next_occurrences_at_checkout', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment