Scheduled Orders: Custom Field Next Occurrence Customization (Date Picker Example)

Adjusts next_occurrence_date based on a custom field value (date picker example).

Important: Always test customizations to your site on a staging environment before making changes to your live / production site.  If you are not experienced with testing changes on a staging site, here is a good article to review.

* Alters the Next Occurrence Date for a Scheduled Order based on custom field.
* @param array $scheduled_order_data The Scheduled Order Data to be sent to API.
* @param int The current WC Order ID.
* @return array The filtered Scheduled Order Data.
function xx_adjust_next_scheduled_order_date( $scheduled_order_data, $order_id  ){

  // Make some call to check the custom Datepicker field & then convert to UTC
  // Date Time String.
  $value = get_post_meta( $order_id, 'xx_custom_date_picker_field' );

  // Adjust the NextOccurrenceUtc to match the custom value.
  // Get the Date into the Date Time String the API expects
  $order_date = new WC_DateTime( $value );
  $scheduled_order_data['NextOccurrenceUtc'] = $order_date->format('Y-m-d\TH:i:s');

  return $scheduled_order_data;

add_filter( 'autoship_create_scheduled_order_data', 'xx_adjust_next_scheduled_order_date', 10, 2 );

Still need help? Contact Us Contact Us