WooCommerce Box Office
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
class WOE_BoxOfficeFields_As_Products { var $fields = array('First Name','Last Name','Email'); // modify names here! Must match to Labels at tab "Ticket Fields" function __construct() { add_filter('woe_get_order_product_fields', function ($fields) { $fields['product_ticket_id'] = array( 'label' => 'Ticket Id', 'colname' => 'Ticket Id', 'checked' => 1 ); foreach($this->fields as $name) $fields['product_'.$name] = array( 'label' => $name, 'colname' => $name, 'checked' => 1 ); return $fields; }); add_filter('woe_fetch_order_products',function ($products,$order,$labels, $format, $static_vals) { $this->line_id = 1; $results = array(); foreach ( $order->get_items('line_item') as $item_id=>$item ) { // seek for assigned tickets $product_data = $products[$item_id]; // take already extracted data for product! $item_meta = get_metadata( 'order_item', $item_id ); $ticket_added = false; foreach($item_meta as $key=>$value) { // parse manual ticket id from HTML! if(preg_match("#ticket-id-(\d+)#", $key, $match) ) { $this->add_client_ticket($results, $match[1],$labels,$product_data); $ticket_added = true; } } if( !$ticket_added ) { if ( isset($product_data[ 'line_id' ]) ) $product_data[ 'line_id' ] = $this->line_id++; $results[] = $product_data; // just add item ,it's not ticket!!! } } return $results; },10,5); }// end __construct function add_client_ticket(&$results, $ticket_id,$labels,$product_data) { $data = array( 'product_ticket_id'=>$ticket_id); $product_id = get_post_meta( $ticket_id, '_product', true ); if ( !$product_id ) return; $ticket_product = wc_get_product( $product_id ); //$data['name'] = $ticket_product->get_title(); foreach ( wc_box_office_get_product_ticket_fields( $product_id ) as $field_key => $field ) { // Replace content placeholders with ticket fields. $field_value = get_post_meta( $ticket_id, $field_key, true ); if ( is_array( $field_value ) ) { $field_value = implode( ', ', $field_value ); } $data[ 'product_'.$field['label'] ] = $field_value; } $this->add_ticket($results, $data,$labels,$product_data); } function add_ticket(&$results, $data,$labels,$product_data) { foreach ( $labels as $field => $label ) { if ( $field == 'line_id' ) { $row[ $field ] = $this->line_id++; } elseif ( isset( $data[$field]) ) { // boxoffice fields $row[ $field ] = $data[$field]; } elseif ( isset( $product_data[ $field ] ) ) { $row[ $field ] = $product_data[ $field ]; } else $row[ $field ] = ''; } $results[] = $row; } } new WOE_BoxOfficeFields_As_Products(); |