Skip to content

Instantly share code, notes, and snippets.

@Garconis
Last active July 30, 2018 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Garconis/8ddf34b711e59c7df918074f8b643ee3 to your computer and use it in GitHub Desktop.
Save Garconis/8ddf34b711e59c7df918074f8b643ee3 to your computer and use it in GitHub Desktop.
Advanced Custom Columns Pro | NASSGAP
<?php
/**
* Display custom Event Attendee field values on the ORDER LIST/TABLE SCREEN via overriding Admin Columns Pro field column
*
* Filter the display value for a column
*
* @param mixed $value Custom field value
* @param int $id Object ID
* @param AC_Column $column Column instance
*/
function fs_custom_attendee_column_value( $value, $id, $column ) {
if ( $column instanceof AC\Column\CustomField ) {
// get the meta key of this column
$meta_key = $column->get_meta_key();
// if the meta key is the FooEvents Attendee Field one that we want data from
if ( 'WooCommerceEventsOrderTickets' == $meta_key ) {
// example of replacing column field value with a value from a different field
// $billingphone = get_post_meta( $id, '_billing_phone', true );
// $value = sprintf( '<span>'. $billingphone .'</span>', $value );
// ok, so we know the current column is the Attendee Ticket field, so lets grab that meta to manipulate it
$event = get_post_meta( $id, 'WooCommerceEventsOrderTickets', true );
// if its not an array then return the value
if( ! is_array($event) ) return $value;
// if there is an array the lets get into it, and set the initial array as a variable
$event = isset($event[1][1]) ? $event[1][1] : '';
// if there are no field data in the array variable spit it out
if( sizeof($event) == 0 ) return $value;
// go into the array of the next custom field (which is in an array itself)
$custom = isset($event['WooCommerceEventsCustomAttendeeFields']) ? $event['WooCommerceEventsCustomAttendeeFields'] : '';
// SET VARIABLES FOR ATTENDEE NAME
// if has first name then set it
if ( isset($event['WooCommerceEventsAttendeeName']) ) {
$attendee_firstname = $event['WooCommerceEventsAttendeeName'];
}
else {
$attendee_firstname = '';
}
// if has last name then set it
if ( isset($event['WooCommerceEventsAttendeeLastName']) ) {
$attendee_lastname = $event['WooCommerceEventsAttendeeLastName'];
}
else {
$attendee_lastname = '';
}
// initially set the full name to blank, in case the attendee didnt have a first or last name set
$attendee_fullname = '';
// if firstname but no lastname
if ( ($attendee_firstname) && (! $attendee_lastname) ) {
$attendee_fullname = $attendee_firstname;
}
// if no firstname but has lastname
if ( (! $attendee_firstname) && ($attendee_lastname) ) {
$attendee_fullname = $attendee_lastname;
}
// if has both names
if ( $attendee_firstname && $attendee_lastname ) {
$attendee_fullname = $attendee_firstname .' '. $attendee_lastname;
}
// SET VARIABLES FOR ATTENDEE ADDRESS
// if has street address then set it
if ( isset($custom['fooevents_custom_address']) ) {
$attendee_address_street = $custom['fooevents_custom_address'] .', ';
}
else {
$attendee_address_street = '';
}
// if has city address then set it
if ( isset($custom['fooevents_custom_city']) ) {
$attendee_address_city = $custom['fooevents_custom_city'] .', ';
}
else {
$attendee_address_city = '';
}
// if has state address then set it
if ( isset($custom['fooevents_custom_state/provinc']) ) {
$attendee_address_state = $custom['fooevents_custom_state/province'] .', ';
}
else {
$attendee_address_state = '';
}
// if has zip code address then set it
if ( isset($custom['fooevents_custom_postal_code']) ) {
$attendee_address_zip = $custom['fooevents_custom_postal_code'];
}
else {
$attendee_address_zip = '';
}
$attendee_address = $attendee_address_street . $attendee_address_city . $attendee_address_state . $attendee_address_zip;
// Set our array of needed data
$fields_array = [
//__('First Name') => isset($event['WooCommerceEventsAttendeeName']) ? $event['WooCommerceEventsAttendeeName'] : '',
//__('Last Name') => isset($event['WooCommerceEventsAttendeeLastName']) ? $event['WooCommerceEventsAttendeeLastName'] : '',
__('Name') => $attendee_fullname,
__('Email') => isset($event['WooCommerceEventsAttendeeEmail']) ? $event['WooCommerceEventsAttendeeEmail'] : '',
__('Phone') => isset($event['WooCommerceEventsAttendeeTelephone']) ? $event['WooCommerceEventsAttendeeTelephone'] : '',
__('Organization') => isset($custom['fooevents_custom_organization']) ? $custom['fooevents_custom_organization'] : '',
__('Title') => isset($custom['fooevents_custom_title']) ? $custom['fooevents_custom_title'] : '',
//__('Address') => isset($custom['fooevents_custom_address']) ? $custom['fooevents_custom_address'] : '',
//__('City') => isset($custom['fooevents_custom_city']) ? $custom['fooevents_custom_city'] : '',
//__('State') => isset($custom['fooevents_custom_state/province']) ? $custom['fooevents_custom_state/province'] : '',
//__('Postal Code') => isset($custom['fooevents_custom_postal_code']) ? $custom['fooevents_custom_postal_code'] : '',
__('Address') => $attendee_address,
];
// if the variable has nothing
if( ! $event ) return $value;
// wrap HTML around the value output
$value = '<table cellspacing="0" cellpadding="0" style="width: 100%;"><tbody>';
// Loop though the data array to set the fields
foreach( $fields_array as $label => $field_value ):
if( ! empty($field_value) ):
// output each field from the array above
$value .= '<tr><td style="padding: 0.25em 0; line-height: 1.25em; font-size: 0.875em;"><strong style="font-weight: 700;">' . $label . '</strong>: ' . $field_value . '</td></tr>';
endif;
endforeach;
// close the HTML wrapper around the value output
$value .= '</tbody></table>';
}
}
// show me the results
return $value;
}
add_filter( 'ac/column/value', 'fs_custom_attendee_column_value', 10, 3 );
/**
* Ensure the custom Event Attendee field values get exported with the rest of the Admin Columns CSV
*
* @param string $value
* @param AC_Column $column
* @param int $post_id
*/
function fs_custom_attendee_column_export( $value, $column, $post_id ) {
if ( $column instanceof ACP\Column\CustomField ) {
// get the meta key of this column
$meta_key = $column->get_meta_key();
// if the meta key is the FooEvents Attendee Field one that we want data from
if ( 'WooCommerceEventsOrderTickets' == $meta_key ) {
// example of replacing column field value with a value from a different field
// $billingphone = get_post_meta( $post_id, '_billing_phone', true );
// $value = sprintf( '<span>'. $billingphone .'</span>', $value );
// ok, so we know the current column is the Attendee Ticket field, so lets grab that meta to manipulate it
$event = get_post_meta( $post_id, 'WooCommerceEventsOrderTickets', true );
// if its not an array then return the value
if( ! is_array($event) ) return $value;
// if there is an array the lets get into it, and set the initial array as a variable
$event = isset($event[1][1]) ? $event[1][1] : '';
// if there are no field data in the array variable spit it out
if( sizeof($event) == 0 ) return $value;
// go into the array of the next custom field (which is in an array itself)
$custom = isset($event['WooCommerceEventsCustomAttendeeFields']) ? $event['WooCommerceEventsCustomAttendeeFields'] : '';
// SET VARIABLES FOR ATTENDEE NAME
// if has first name then set it
if ( isset($event['WooCommerceEventsAttendeeName']) ) {
$attendee_firstname = $event['WooCommerceEventsAttendeeName'];
}
else {
$attendee_firstname = '';
}
// if has last name then set it
if ( isset($event['WooCommerceEventsAttendeeLastName']) ) {
$attendee_lastname = $event['WooCommerceEventsAttendeeLastName'];
}
else {
$attendee_lastname = '';
}
// initially set the full name to blank, in case the attendee didnt have a first or last name set
$attendee_fullname = '';
// if firstname but no lastname
if ( ($attendee_firstname) && (! $attendee_lastname) ) {
$attendee_fullname = $attendee_firstname;
}
// if no firstname but has lastname
if ( (! $attendee_firstname) && ($attendee_lastname) ) {
$attendee_fullname = $attendee_lastname;
}
// if has both names
if ( $attendee_firstname && $attendee_lastname ) {
$attendee_fullname = $attendee_firstname .' '. $attendee_lastname;
}
// SET VARIABLES FOR ATTENDEE ADDRESS
// if has street address then set it
if ( isset($custom['fooevents_custom_address']) ) {
$attendee_address_street = $custom['fooevents_custom_address'] .', ';
}
else {
$attendee_address_street = '';
}
// if has city address then set it
if ( isset($custom['fooevents_custom_city']) ) {
$attendee_address_city = $custom['fooevents_custom_city'] .', ';
}
else {
$attendee_address_city = '';
}
// if has state address then set it
if ( isset($custom['fooevents_custom_state/provinc']) ) {
$attendee_address_state = $custom['fooevents_custom_state/province'] .', ';
}
else {
$attendee_address_state = '';
}
// if has zip code address then set it
if ( isset($custom['fooevents_custom_postal_code']) ) {
$attendee_address_zip = $custom['fooevents_custom_postal_code'];
}
else {
$attendee_address_zip = '';
}
$attendee_address = $attendee_address_street . $attendee_address_city . $attendee_address_state . $attendee_address_zip;
// Set our array of needed data
$fields_array = [
__('First Name') => isset($event['WooCommerceEventsAttendeeName']) ? $event['WooCommerceEventsAttendeeName'] : '',
__('Last Name') => isset($event['WooCommerceEventsAttendeeLastName']) ? $event['WooCommerceEventsAttendeeLastName'] : '',
//__('Name') => $attendee_fullname,
__('Email') => isset($event['WooCommerceEventsAttendeeEmail']) ? $event['WooCommerceEventsAttendeeEmail'] : '',
__('Phone') => isset($event['WooCommerceEventsAttendeeTelephone']) ? $event['WooCommerceEventsAttendeeTelephone'] : '',
__('Organization') => isset($custom['fooevents_custom_organization']) ? $custom['fooevents_custom_organization'] : '',
__('Title') => isset($custom['fooevents_custom_title']) ? $custom['fooevents_custom_title'] : '',
__('Address') => isset($custom['fooevents_custom_address']) ? $custom['fooevents_custom_address'] : '',
__('City') => isset($custom['fooevents_custom_city']) ? $custom['fooevents_custom_city'] : '',
__('State') => isset($custom['fooevents_custom_state/province']) ? $custom['fooevents_custom_state/province'] : '',
__('Postal Code') => isset($custom['fooevents_custom_postal_code']) ? $custom['fooevents_custom_postal_code'] : '',
//__('Address') => $attendee_address,
];
// if the variable has nothing
if( ! $event ) return $value;
// wrap HTML around the value output
$value = '';
// Loop though the data array to set the fields
foreach( $fields_array as $label => $field_value ):
if( ! empty($field_value) ):
// output each field from the array above
$value .= $label . ': ' . $field_value . '; ';
endif;
endforeach;
// close the HTML wrapper around the value output
$value .= '';
}
}
// show me the results
return $value;
}
add_filter( 'ac/export/value', 'fs_custom_attendee_column_export', 10, 3 );
/**
* Display custom Event Registration details (item meta data from the order) (which are captured via WooCommerce Add-ons form/fields) on the ORDER LIST/TABLE SCREEN via overriding Admin Columns Pro field column
*
* Filter the display value for a column
*
* @param mixed $value Custom field value
* @param int $id Object ID
* @param AC_Column $column Column instance
*/
function fs_custom_event_registration_details_column_value( $value, $id, $column ) {
if ( $column instanceof AC\Column\CustomField ) {
$order = wc_get_order( $id );
//$order_items = $order->get_items();
// get the meta key of this column
$meta_key = $column->get_meta_key();
// we've added the WooCommerceEventsTicketsPurchased as a custom column via Admin Columns plugin, and now are targetting that one to override it with different data
if ( 'WooCommerceEventsTicketsPurchased' == $meta_key ) {
// $value is equal to the original value, so we use $value .= to add (append) additional data to it */
if ( $value == 1) {
$value .= ' event item<br>';
}
elseif ( $value > 1) {
$value .= ' event items<br>';
}
foreach ( $order->get_items() as $item_id => $item ) {
$product_name = $item->get_name();
//$product_id = $item->get_product_id();
//$product_variation_id = $item->get_variation_id();
$product_id = $item['product_id'];
// only for items within the Events category
if ( has_term( 'events', 'product_cat', $product_id ) ) {
// wrap HTML around the value output
$value .= '<table cellspacing="0" cellpadding="0" style="width: 100%; border: 1px solid rgba(0,0,0,0.66); margin: 0.5em 0;">';
$value .= '<thead style="background: rgba(0,0,0,0.1);"><tr><th style="padding: 0.25em; line-height: 1.25em; font-size: 0.875em;">
<h5 style="line-height: 1.2em; font-size: 1em; margin: 0; padding: 0; font-weight: 700;">'. $product_name .'</h5>
</th></tr></thead>';
$value .= '<tbody>';
// grab the meta from the item
$meta_data = $item->get_formatted_meta_data( '' );
// Loop though the data array to set the fields
foreach ( $meta_data as $meta_id => $meta ) :
// grab the key and strip anything that may be junky
$field_display_label = wp_kses_post( $meta->display_key );
// grab the key value and stip anything that may be junky, as well as ensuring things are properly wrapped
$field_display_value = wp_kses_post( force_balance_tags( $meta->display_value ) );
// by default, the key value has p tags wrapping it, so lets remove those for now
$field_display_value = str_replace('<p>','',$field_display_value);
$field_display_value = str_replace('</p>','',$field_display_value);
$value .= '<tr><td style="padding: 0.25em; line-height: 1.25em; font-size: 0.875em; border-top: 1px dotted rgba(0,0,0,0.25);">
<strong style="font-weight: 700;">'. $field_display_label .'</strong>: <br>&nbsp;&bull;&nbsp;'. $field_display_value .'</td></tr>';
endforeach;
// close the HTML wrapper around the value output
$value .= '</tbody></table>';
}
}
}
}
// show me the results
return $value;
}
add_filter( 'ac/column/value', 'fs_custom_event_registration_details_column_value', 10, 3 );
/**
* Ensure the custom Event Attendee field values get exported with the rest of the Admin Columns CSV
*
* @param string $value
* @param AC_Column $column
* @param int $post_id
*/
function fs_custom_event_registration_details_column_export( $value, $column, $post_id ) {
if ( $column instanceof AC\Column\CustomField ) {
$order = wc_get_order( $post_id );
//$order_items = $order->get_items();
// get the meta key of this column
$meta_key = $column->get_meta_key();
// we've added the WooCommerceEventsTicketsPurchased as a custom column via Admin Columns plugin, and now are targetting that one to override it with different data
if ( 'WooCommerceEventsTicketsPurchased' == $meta_key ) {
foreach ( $order->get_items() as $item_id => $item ) {
$product_name = $item->get_name();
//$product_id = $item->get_product_id();
//$product_variation_id = $item->get_variation_id();
$product_id = $item['product_id'];
// only for items within the Events category
if ( has_term( 'events', 'product_cat', $product_id ) ) {
// wrap HTML around the value output
//$value = '';
$value .= '*** '. $product_name .' *** details; ';
// grab the meta from the item
$meta_data = $item->get_formatted_meta_data( '' );
// Loop though the data array to set the fields
foreach ( $meta_data as $meta_id => $meta ) :
// grab the key and strip anything that may be junky
$field_display_label = wp_kses_post( $meta->display_key );
// grab the key value and stip anything that may be junky, as well as ensuring things are properly wrapped
$field_display_value = wp_kses_post( force_balance_tags( $meta->display_value ) );
// by default, the key value has p tags wrapping it, so lets remove those for now
$field_display_value = str_replace('<p>','',$field_display_value);
$field_display_value = str_replace('</p>','',$field_display_value);
$value .= $field_display_label .': '. $field_display_value .'; ';
endforeach;
// close the HTML wrapper around the value output
$value .= '';
}
}
}
}
// show me the results
return $value;
}
add_filter( 'ac/export/value', 'fs_custom_event_registration_details_column_export', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment