Skip to content

Instantly share code, notes, and snippets.

@cdharrison
Created May 14, 2018 21:50
Show Gist options
  • Save cdharrison/b97070890395dc796c35f379afbf76b0 to your computer and use it in GitHub Desktop.
Save cdharrison/b97070890395dc796c35f379afbf76b0 to your computer and use it in GitHub Desktop.
This adds the ability for FooEvents data to be automatically detected and mapped properly when importing a CSV of Events-as-Products for WooCommerce.
<?php
/**
* Register FooEvents columns in the importer.
* @link: https://github.com/woocommerce/woocommerce/wiki/Product-CSV-Importer-&-Exporter#adding-custom-import-columns-developers
*
* @param array $options
* @return array $options
*/
function add_column_to_importer( $options ) {
// column slug => column name
$options['fooevents_custom_attendee_fields_options_serialized'] = 'fooevents_custom_attendee_fields_options_serialized';
$options['FooEventsPDFTicketsEmailText'] = 'FooEventsPDFTicketsEmailText';
$options['FooEventsTicketFooterText'] = 'FooEventsTicketFooterText';
$options['WooCommerceEventsEvent'] = 'WooCommerceEventsEvent';
$options['WooCommerceEventsDate'] = 'WooCommerceEventsDate';
$options['WooCommerceEventsDateTimestamp'] = 'WooCommerceEventsDateTimestamp';
$options['WooCommerceEventsHour'] = 'WooCommerceEventsHour';
$options['WooCommerceEventsMinutes'] = 'WooCommerceEventsMinutes';
$options['WooCommerceEventsPeriod'] = 'WooCommerceEventsPeriod';
$options['WooCommerceEventsLocation'] = 'WooCommerceEventsLocation';
$options['WooCommerceEventsTicketLogo'] = 'WooCommerceEventsTicketLogo';
$options['WooCommerceEventsTicketHeaderImage'] = 'WooCommerceEventsTicketHeaderImage';
$options['WooCommerceEventsTicketText'] = 'WooCommerceEventsTicketText';
$options['WooCommerceEventsThankYouText'] = 'WooCommerceEventsThankYouText';
$options['WooCommerceEventsSupportContact'] = 'WooCommerceEventsSupportContact';
$options['WooCommerceEventsHourEnd'] = 'WooCommerceEventsHourEnd';
$options['WooCommerceEventsMinutesEnd'] = 'WooCommerceEventsMinutesEnd';
$options['WooCommerceEventsEndPeriod'] = 'WooCommerceEventsEndPeriod';
$options['WooCommerceEventsGPS'] = 'WooCommerceEventsGPS';
$options['WooCommerceEventsDirections'] = 'WooCommerceEventsDirections';
$options['WooCommerceEventsEmail'] = 'WooCommerceEventsEmail';
$options['WooCommerceEventsTicketBackgroundColor'] = 'WooCommerceEventsTicketBackgroundColor';
$options['WooCommerceEventsTicketButtonColor'] = 'WooCommerceEventsTicketButtonColor';
$options['WooCommerceEventsTicketTextColor'] = 'WooCommerceEventsTicketTextColor';
$options['WooCommerceEventsBackgroundColor'] = 'WooCommerceEventsBackgroundColor';
$options['WooCommerceEventsTextColor'] = 'WooCommerceEventsTextColor';
$options['WooCommerceEventsGoogleMaps'] = 'WooCommerceEventsGoogleMaps';
$options['WooCommerceEventsTicketPurchaserDetails'] = 'WooCommerceEventsTicketPurchaserDetails';
$options['WooCommerceEventsTicketAddCalendar'] = 'WooCommerceEventsTicketAddCalendar';
$options['WooCommerceEventsTicketDisplayDateTime'] = 'WooCommerceEventsTicketDisplayDateTime';
$options['WooCommerceEventsTicketDisplayBarcode'] = 'WooCommerceEventsTicketDisplayBarcode';
$options['WooCommerceEventsTicketDisplayPrice'] = 'WooCommerceEventsTicketDisplayPrice';
$options['WooCommerceEventsCaptureAttendeeDetails'] = 'WooCommerceEventsCaptureAttendeeDetails';
$options['WooCommerceEventsEmailAttendee'] = 'WooCommerceEventsEmailAttendee';
$options['WooCommerceEventsCaptureAttendeeTelephone'] = 'WooCommerceEventsCaptureAttendeeTelephone';
$options['WooCommerceEventsCaptureAttendeeCompany'] = 'WooCommerceEventsCaptureAttendeeCompany';
$options['WooCommerceEventsCaptureAttendeeDesignation'] = 'WooCommerceEventsCaptureAttendeeDesignation';
$options['WooCommerceEventsSendEmailTickets'] = 'WooCommerceEventsSendEmailTickets';
$options['WooCommerceEventsEmailSubjectSingle'] = 'WooCommerceEventsEmailSubjectSingle';
$options['WooCommerceEventsExportUnpaidTickets'] = 'WooCommerceEventsExportUnpaidTickets';
$options['WooCommerceEventsExportBillingDetails'] = 'WooCommerceEventsExportBillingDetails';
$options['WooCommerceEventsTicketTheme'] = 'WooCommerceEventsTicketTheme';
$options['WooCommerceEventsAttendeeOverride'] = 'WooCommerceEventsAttendeeOverride';
$options['WooCommerceEventsTicketOverride'] = 'WooCommerceEventsTicketOverride';
$options['WooCommerceEventsEndDate'] = 'WooCommerceEventsEndDate';
$options['WooCommerceEventsEndDateTimestamp'] = 'WooCommerceEventsEndDateTimestamp';
$options['WooCommerceEventsMultiDayType'] = 'WooCommerceEventsMultiDayType';
$options['WooCommerceEventsNumDays'] = 'WooCommerceEventsNumDays';
$options['WooCommerceEventsDayOverride'] = 'WooCommerceEventsDayOverride';
return $options;
}
add_filter( 'woocommerce_csv_product_import_mapping_options', 'add_column_to_importer' );
/**
* Add automatic mapping support for 'Custom Column'.
* This will automatically select the correct mapping for columns named 'Custom Column' or 'custom column'.
*
* @param array $columns
* @return array $columns
*/
function add_column_to_mapping_screen( $columns ) {
$columns['fooevents_custom_attendee_fields_options_serialized'] = 'fooevents_custom_attendee_fields_options_serialized';
$columns['FooEventsPDFTicketsEmailText'] = 'FooEventsPDFTicketsEmailText';
$columns['FooEventsTicketFooterText'] = 'FooEventsTicketFooterText';
$columns['WooCommerceEventsEvent'] = 'WooCommerceEventsEvent';
$columns['WooCommerceEventsDate'] = 'WooCommerceEventsDate';
$columns['WooCommerceEventsDateTimestamp'] = 'WooCommerceEventsDateTimestamp';
$columns['WooCommerceEventsHour'] = 'WooCommerceEventsHour';
$columns['WooCommerceEventsMinutes'] = 'WooCommerceEventsMinutes';
$columns['WooCommerceEventsPeriod'] = 'WooCommerceEventsPeriod';
$columns['WooCommerceEventsLocation'] = 'WooCommerceEventsLocation';
$columns['WooCommerceEventsTicketLogo'] = 'WooCommerceEventsTicketLogo';
$columns['WooCommerceEventsTicketHeaderImage'] = 'WooCommerceEventsTicketHeaderImage';
$columns['WooCommerceEventsTicketText'] = 'WooCommerceEventsTicketText';
$columns['WooCommerceEventsThankYouText'] = 'WooCommerceEventsThankYouText';
$columns['WooCommerceEventsSupportContact'] = 'WooCommerceEventsSupportContact';
$columns['WooCommerceEventsHourEnd'] = 'WooCommerceEventsHourEnd';
$columns['WooCommerceEventsMinutesEnd'] = 'WooCommerceEventsMinutesEnd';
$columns['WooCommerceEventsEndPeriod'] = 'WooCommerceEventsEndPeriod';
$columns['WooCommerceEventsGPS'] = 'WooCommerceEventsGPS';
$columns['WooCommerceEventsDirections'] = 'WooCommerceEventsDirections';
$columns['WooCommerceEventsEmail'] = 'WooCommerceEventsEmail';
$columns['WooCommerceEventsTicketBackgroundColor'] = 'WooCommerceEventsTicketBackgroundColor';
$columns['WooCommerceEventsTicketButtonColor'] = 'WooCommerceEventsTicketButtonColor';
$columns['WooCommerceEventsTicketTextColor'] = 'WooCommerceEventsTicketTextColor';
$columns['WooCommerceEventsBackgroundColor'] = 'WooCommerceEventsBackgroundColor';
$columns['WooCommerceEventsTextColor'] = 'WooCommerceEventsTextColor';
$columns['WooCommerceEventsGoogleMaps'] = 'WooCommerceEventsGoogleMaps';
$columns['WooCommerceEventsTicketPurchaserDetails'] = 'WooCommerceEventsTicketPurchaserDetails';
$columns['WooCommerceEventsTicketAddCalendar'] = 'WooCommerceEventsTicketAddCalendar';
$columns['WooCommerceEventsTicketDisplayDateTime'] = 'WooCommerceEventsTicketDisplayDateTime';
$columns['WooCommerceEventsTicketDisplayBarcode'] = 'WooCommerceEventsTicketDisplayBarcode';
$columns['WooCommerceEventsTicketDisplayPrice'] = 'WooCommerceEventsTicketDisplayPrice';
$columns['WooCommerceEventsCaptureAttendeeDetails'] = 'WooCommerceEventsCaptureAttendeeDetails';
$columns['WooCommerceEventsEmailAttendee'] = 'WooCommerceEventsEmailAttendee';
$columns['WooCommerceEventsCaptureAttendeeTelephone'] = 'WooCommerceEventsCaptureAttendeeTelephone';
$columns['WooCommerceEventsCaptureAttendeeCompany'] = 'WooCommerceEventsCaptureAttendeeCompany';
$columns['WooCommerceEventsCaptureAttendeeDesignation'] = 'WooCommerceEventsCaptureAttendeeDesignation';
$columns['WooCommerceEventsSendEmailTickets'] = 'WooCommerceEventsSendEmailTickets';
$columns['WooCommerceEventsEmailSubjectSingle'] = 'WooCommerceEventsEmailSubjectSingle';
$columns['WooCommerceEventsExportUnpaidTickets'] = 'WooCommerceEventsExportUnpaidTickets';
$columns['WooCommerceEventsExportBillingDetails'] = 'WooCommerceEventsExportBillingDetails';
$columns['WooCommerceEventsTicketTheme'] = 'WooCommerceEventsTicketTheme';
$columns['WooCommerceEventsAttendeeOverride'] = 'WooCommerceEventsAttendeeOverride';
$columns['WooCommerceEventsTicketOverride'] = 'WooCommerceEventsTicketOverride';
$columns['WooCommerceEventsEndDate'] = 'WooCommerceEventsEndDate';
$columns['WooCommerceEventsEndDateTimestamp'] = 'WooCommerceEventsEndDateTimestamp';
$columns['WooCommerceEventsMultiDayType'] = 'WooCommerceEventsMultiDayType';
$columns['WooCommerceEventsNumDays'] = 'WooCommerceEventsNumDays';
$columns['WooCommerceEventsDayOverride'] = 'WooCommerceEventsDayOverride';
return $columns;
}
add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'add_column_to_mapping_screen' );
/**
* Process the data read from the CSV file.
* This just saves the value in meta data, but you can do anything you want here with the data.
*
* @param WC_Product $object - Product being imported or updated.
* @param array $data - CSV data read for the product.
* @return WC_Product $object
*/
function process_import( $object, $data ) {
if ( ! empty( $data['fooevents_custom_attendee_fields_options_serialized'] ) )
$object->update_meta_data( 'fooevents_custom_attendee_fields_options_serialized', $data['fooevents_custom_attendee_fields_options_serialized'] );
if ( ! empty( $data['FooEventsPDFTicketsEmailText'] ) )
$object->update_meta_data( 'FooEventsPDFTicketsEmailText', $data['FooEventsPDFTicketsEmailText'] );
if ( ! empty( $data['FooEventsTicketFooterText'] ) )
$object->update_meta_data( 'FooEventsTicketFooterText', $data['FooEventsTicketFooterText'] );
if ( ! empty( $data['WooCommerceEventsEvent'] ) )
$object->update_meta_data( 'WooCommerceEventsEvent', $data['WooCommerceEventsEvent'] );
if ( ! empty( $data['WooCommerceEventsDate'] ) )
$object->update_meta_data( 'WooCommerceEventsDate', $data['WooCommerceEventsDate'] );
if ( ! empty( $data['WooCommerceEventsDateTimestamp'] ) )
$object->update_meta_data( 'WooCommerceEventsDateTimestamp', $data['WooCommerceEventsDateTimestamp'] );
if ( ! empty( $data['WooCommerceEventsHour'] ) )
$object->update_meta_data( 'WooCommerceEventsHour', $data['WooCommerceEventsHour'] );
if ( ! empty( $data['WooCommerceEventsMinutes'] ) )
$object->update_meta_data( 'WooCommerceEventsMinutes', $data['WooCommerceEventsMinutes'] );
if ( ! empty( $data['WooCommerceEventsPeriod'] ) )
$object->update_meta_data( 'WooCommerceEventsPeriod', $data['WooCommerceEventsPeriod'] );
if ( ! empty( $data['WooCommerceEventsLocation'] ) )
$object->update_meta_data( 'WooCommerceEventsLocation', $data['WooCommerceEventsLocation'] );
if ( ! empty( $data['WooCommerceEventsTicketLogo'] ) )
$object->update_meta_data( 'WooCommerceEventsTicketLogo', $data['WooCommerceEventsTicketLogo'] );
if ( ! empty( $data['WooCommerceEventsTicketHeaderImag'] ) )
$object->update_meta_data( 'WooCommerceEventsTicketHeaderImage', $data['WooCommerceEventsTicketHeaderImage'] );
if ( ! empty( $data['WooCommerceEventsTicketText'] ) )
$object->update_meta_data( 'WooCommerceEventsTicketText', $data['WooCommerceEventsTicketText'] );
if ( ! empty( $data['WooCommerceEventsThankYouText'] ) )
$object->update_meta_data( 'WooCommerceEventsThankYouText', $data['WooCommerceEventsThankYouText'] );
if ( ! empty( $data['WooCommerceEventsSupportContact'] ) )
$object->update_meta_data( 'WooCommerceEventsSupportContact', $data['WooCommerceEventsSupportContact'] );
if ( ! empty( $data['WooCommerceEventsHourEnd'] ) )
$object->update_meta_data( 'WooCommerceEventsHourEnd', $data['WooCommerceEventsHourEnd'] );
if ( ! empty( $data['WooCommerceEventsMinutesEnd'] ) )
$object->update_meta_data( 'WooCommerceEventsMinutesEnd', $data['WooCommerceEventsMinutesEnd'] );
if ( ! empty( $data['WooCommerceEventsEndPeriod'] ) )
$object->update_meta_data( 'WooCommerceEventsEndPeriod', $data['WooCommerceEventsEndPeriod'] );
if ( ! empty( $data['WooCommerceEventsGPS'] ) )
$object->update_meta_data( 'WooCommerceEventsGPS', $data['WooCommerceEventsGPS'] );
if ( ! empty( $data['WooCommerceEventsDirections'] ) )
$object->update_meta_data( 'WooCommerceEventsDirections', $data['WooCommerceEventsDirections'] );
if ( ! empty( $data['WooCommerceEventsEmail'] ) )
$object->update_meta_data( 'WooCommerceEventsEmail', $data['WooCommerceEventsEmail'] );
if ( ! empty( $data['WooCommerceEventsTicketBackgroundColor'] ) )
$object->update_meta_data( 'WooCommerceEventsTicketBackgroundColor', $data['WooCommerceEventsTicketBackgroundColor'] );
if ( ! empty( $data['WooCommerceEventsTicketButtonColor'] ) )
$object->update_meta_data( 'WooCommerceEventsTicketButtonColor', $data['WooCommerceEventsTicketButtonColor'] );
if ( ! empty( $data['WooCommerceEventsTicketTextColor'] ) )
$object->update_meta_data( 'WooCommerceEventsTicketTextColor', $data['WooCommerceEventsTicketTextColor'] );
if ( ! empty( $data['WooCommerceEventsBackgroundColor'] ) )
$object->update_meta_data( 'WooCommerceEventsBackgroundColor', $data['WooCommerceEventsBackgroundColor'] );
if ( ! empty( $data['WooCommerceEventsTextColor'] ) )
$object->update_meta_data( 'WooCommerceEventsTextColor', $data['WooCommerceEventsTextColor'] );
if ( ! empty( $data['WooCommerceEventsGoogleMaps'] ) )
$object->update_meta_data( 'WooCommerceEventsGoogleMaps', $data['WooCommerceEventsGoogleMaps'] );
if ( ! empty( $data['WooCommerceEventsTicketPurchaserDetails'] ) )
$object->update_meta_data( 'WooCommerceEventsTicketPurchaserDetails', $data['WooCommerceEventsTicketPurchaserDetails'] );
if ( ! empty( $data['WooCommerceEventsTicketAddCalendar'] ) )
$object->update_meta_data( 'WooCommerceEventsTicketAddCalendar', $data['WooCommerceEventsTicketAddCalendar'] );
if ( ! empty( $data['WooCommerceEventsTicketDisplayDateTime'] ) )
$object->update_meta_data( 'WooCommerceEventsTicketDisplayDateTime', $data['WooCommerceEventsTicketDisplayDateTime'] );
if ( ! empty( $data['WooCommerceEventsTicketDisplayBarcode'] ) )
$object->update_meta_data( 'WooCommerceEventsTicketDisplayBarcode', $data['WooCommerceEventsTicketDisplayBarcode'] );
if ( ! empty( $data['WooCommerceEventsTicketDisplayPrice'] ) )
$object->update_meta_data( 'WooCommerceEventsTicketDisplayPrice', $data['WooCommerceEventsTicketDisplayPrice'] );
if ( ! empty( $data['WooCommerceEventsCaptureAttendeeDetails'] ) )
$object->update_meta_data( 'WooCommerceEventsCaptureAttendeeDetails', $data['WooCommerceEventsCaptureAttendeeDetails'] );
if ( ! empty( $data['WooCommerceEventsEmailAttendee'] ) )
$object->update_meta_data( 'WooCommerceEventsEmailAttendee', $data['WooCommerceEventsEmailAttendee'] );
if ( ! empty( $data['WooCommerceEventsCaptureAttendeeTelephone'] ) )
$object->update_meta_data( 'WooCommerceEventsCaptureAttendeeTelephone', $data['WooCommerceEventsCaptureAttendeeTelephone'] );
if ( ! empty( $data['WooCommerceEventsCaptureAttendeeCompany'] ) )
$object->update_meta_data( 'WooCommerceEventsCaptureAttendeeCompany', $data['WooCommerceEventsCaptureAttendeeCompany'] );
if ( ! empty( $data['WooCommerceEventsCaptureAttendeeDesignation'] ) )
$object->update_meta_data( 'WooCommerceEventsCaptureAttendeeDesignation', $data['WooCommerceEventsCaptureAttendeeDesignation'] );
if ( ! empty( $data['WooCommerceEventsSendEmailTickets'] ) )
$object->update_meta_data( 'WooCommerceEventsSendEmailTickets', $data['WooCommerceEventsSendEmailTickets'] );
if ( ! empty( $data['WooCommerceEventsEmailSubjectSingle'] ) )
$object->update_meta_data( 'WooCommerceEventsEmailSubjectSingle', $data['WooCommerceEventsEmailSubjectSingle'] );
if ( ! empty( $data['WooCommerceEventsExportUnpaidTickets'] ) )
$object->update_meta_data( 'WooCommerceEventsExportUnpaidTickets', $data['WooCommerceEventsExportUnpaidTickets'] );
if ( ! empty( $data['WooCommerceEventsExportBillingDetails'] ) )
$object->update_meta_data( 'WooCommerceEventsExportBillingDetails', $data['WooCommerceEventsExportBillingDetails'] );
if ( ! empty( $data['WooCommerceEventsTicketTheme'] ) )
$object->update_meta_data( 'WooCommerceEventsTicketTheme', $data['WooCommerceEventsTicketTheme'] );
if ( ! empty( $data['WooCommerceEventsAttendeeOverride'] ) )
$object->update_meta_data( 'WooCommerceEventsAttendeeOverride', $data['WooCommerceEventsAttendeeOverride'] );
if ( ! empty( $data['WooCommerceEventsTicketOverride'] ) )
$object->update_meta_data( 'WooCommerceEventsTicketOverride', $data['WooCommerceEventsTicketOverride'] );
if ( ! empty( $data['WooCommerceEventsEndDate'] ) )
$object->update_meta_data( 'WooCommerceEventsEndDate', $data['WooCommerceEventsEndDate'] );
if ( ! empty( $data['WooCommerceEventsEndDateTimestamp'] ) )
$object->update_meta_data( 'WooCommerceEventsEndDateTimestamp', $data['WooCommerceEventsEndDateTimestamp'] );
if ( ! empty( $data['WooCommerceEventsMultiDayType'] ) )
$object->update_meta_data( 'WooCommerceEventsMultiDayType', $data['WooCommerceEventsMultiDayType'] );
if ( ! empty( $data['WooCommerceEventsNumDays'] ) )
$object->update_meta_data( 'WooCommerceEventsNumDays', $data['WooCommerceEventsNumDays'] );
if ( ! empty( $data['WooCommerceEventsDayOverride'] ) )
$object->update_meta_data( 'WooCommerceEventsDayOverride', $data['WooCommerceEventsDayOverride'] );
return $object;
}
add_filter( 'woocommerce_product_import_pre_insert_product_object', 'process_import', 10, 2 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment