Woocommerce sort columns by Event Time

I’m working with WordPress, Woocommerce & Eventon plugin in order to create a selling tickets for events site. Users can order tickets for different dates.

I’m trying to implement a sortable column wich shows for every ticket sold the date for the event (stored in woocommerce_order_itemmeta table as meta_value of meta_key ‘Event-Time’). So first of all I created and populated that column like this:

Read More
add_filter( 'manage_edit-shop_order_columns', 'wooc_set_custom_column_order_columns');

//create custom woocommerce columns
function wooc_set_custom_column_order_columns($columns) {@
$nieuwearray = array();
 foreach($columns as $key => $title) {
   if ($key=='billing_address') { // in front of the Billing column
    $nieuwearray['order_product']  = __( 'Products', 'woocommerce' );
    $nieuwearray['Event-Time']  = __( 'Reservation Date', 'woocommerce' );
   }
   $nieuwearray[$key] = $title;
  }
    return $nieuwearray ;
}

//populate custom woocommerce columns
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );

function custom_shop_order_column( $column ) {
 global $post, $woocommerce, $the_order;

    switch ( $column ) {

        case 'order_product' :
        $terms = $the_order->get_items();
        if ( is_array( $terms ) ) {
            foreach($terms as $term)
            {
                echo $term['item_meta']['_qty'][0] .' x ' . $term['name'] .'<br />';
            }
        } else {
            _e( 'Unable get the product', 'woocommerce' );
        }
        break;

        case 'Event-Time' :
        $terms = $the_order->get_items();
        if ( is_array( $terms ) ) {
            foreach($terms as $term) {
                if(array_key_exists( 'Event-Time', $term ) ){
                    $date_event = substr($term['item_meta']['Event-Time'][0], 0, 10);
                    echo  $date_event . '<br />';
                } else {
                    echo '<b>' . __('No existe fecha reserva?', 'woocommerce') . '</b>';
                }   
            }
        } else {
            _e( 'Unable get the product', 'woocommerce' );
        }
        break;
    }
}

In order to make Event-Time column sortable, I added this code:

add_filter( "manage_edit-shop_order_sortable_columns", 'sort_columns_woocommerce' );
    function sort_columns_woocommerce( $columns ) {
        $custom = array(
            //'order_producten'    => 'MY_COLUMN_1_POST_META_ID',
            'Event-Time'    =>  'Event-Time'
        );
        return wp_parse_args( $custom, $columns );
    }

but nothing happened, so after a few hours looking it up on Google, I found similar solution that I tried to adapt to my case:

function order_by_event_time_join($query) {
    global $wpdb;

    if ( is_admin() && (isset($_GET['post_type']) && $_GET['post_type'] === 'shop_order') && (isset($_GET['orderby']) && $_GET['orderby'] === 'Event-Time') ) {
        $first_item_in_order = "SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = $wpdb->posts.ID AND order_item_type = 'line_item' LIMIT 0, 1";
        $query .= "LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS items ON $wpdb->posts.ID = items.order_id AND items.order_item_id = ($first_item_in_order) ";
        $query .= "LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS itemmeta ON items.order_item_id = itemmeta.order_item_id AND (itemmeta.meta_key = 'Event-Time') ";
    }

    return $query;
}

add_filter('posts_join', 'order_by_event_time_join');


function order_by_event_time_where($where) {
    global $wpdb;

    if( is_admin() && $_GET['post_type'] === 'shop_order' && (isset($_GET['orderby']) && $_GET['orderby'] === 'Event-Time') ) {
        if(strpos($where, 'shop_webhook')) {
            return " AND $wpdb->posts.post_type = 'shop_order' AND itemmeta.meta_key = 'Event-Time'";
        }
    }

    return $where;
}

add_filter('posts_where', 'order_by_event_time_where');

But no luck… What I understand the problem is I’m not able to sort columns by the meta_value ‘Event-Time’ in the ticket-orders section of woocommerce…
I can show Event-Time data but impossible to sort/filter it.

Related posts

Leave a Reply