Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save verygoodplugins/0529cc21c8d51b98acc8c0f65e7e460f to your computer and use it in GitHub Desktop.
Save verygoodplugins/0529cc21c8d51b98acc8c0f65e7e460f to your computer and use it in GitHub Desktop.
Syncs FooEvents event data to separate custom fields on the attendee's contact record for each event.
<?php
/**
* Utility function for getting any FooEvents attendees from a WooCommerce order
* @param WC_Order $order
* @return array Attendees
*/
function get_foo_attendees_from_order( $order ) {
$attendees = array();
$order_data = $order->get_data();
foreach ( $order_data['meta_data'] as $meta ) {
if ( ! is_a( $meta, 'WC_Meta_Data' ) ) {
continue;
}
$data = $meta->get_data();
if ( 'WooCommerceEventsOrderTickets' != $data['key'] ) {
continue;
}
foreach ( $data['value'] as $sub_value ) {
if ( ! is_array( $sub_value ) ) {
continue;
}
foreach ( $sub_value as $attendee ) {
$attendees[] = $attendee;
}
}
}
return $attendees;
}
/**
* Retrieves attendees from the given order
* @param mixed $order_id
* @return array Processed Attendee Date
*/
function get_foo_event_attendees( $order_id){
$order = wc_get_order( $order_id );
$attendees = get_foo_attendees_from_order( $order );
$event_count = [];
$wpf_processed_data = [];
foreach($attendees as $attendee){
$email = trim(strtolower($attendee['WooCommerceEventsAttendeeEmail']));
$product_id = $attendee['WooCommerceEventsProductID'];
$date = str_replace("/","-",$attendee['WooCommerceEventsBookingOptions']['date_label']);
$slot = $attendee['WooCommerceEventsBookingOptions']['slot_label'];
if (isset($event_count[$email])){
$event_count[$email]++;
}else{
$event_count[$email] = 1;
}
$wpf_processed_data[$email][$product_id][$date][$slot] = [
'index' => $event_count[$email],
];
}
return $wpf_processed_data;
}
/**
* Pushes updated data in to relevant WP Fusion fields
* @param $update_data
* @param $attendee
* @param $order_id
* @return mixed
*/
function sync_booking_information( $update_data, $attendee, $order_id ) {
$attendee_data = get_foo_event_attendees($order_id);
$email = trim(strtolower($attendee['WooCommerceEventsAttendeeEmail']));
$product_id = $attendee['WooCommerceEventsProductID'];
$date = str_replace("/","-",$attendee['WooCommerceEventsBookingOptions']['date_label']);
$slot = $attendee['WooCommerceEventsBookingOptions']['slot_label'];
$index = 1;
if (isset($attendee_data[$email][$product_id][$date][$slot]['index'])){
$index = $attendee_data[$email][$product_id][$date][$slot]['index'];
}
$event_name = $update_data['event_name'];
$booking_date = date('Y-m-d' , strtotime(str_replace("/","-",$update_data['booking_date'])));
$booking_time = $update_data['booking_time'];
$zoom_join_url = "";
if (isset($update_data['zoom_join_url'])){
$zoom_join_url = $update_data['zoom_join_url'];
}
$update_data[ 'ac_foo_event_name_' . $index ] = $event_name;
$update_data[ 'ac_foo_event_date_' . $index ] = $booking_date;
$update_data[ 'ac_foo_event_time_' . $index ] = $booking_time;
$update_data[ 'ac_foo_event_zoom_' . $index ] = $zoom_join_url;
return $update_data;
}
add_action( 'wpf_woocommerce_attendee_data', 'sync_booking_information', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment