2

I've run into a really odd issue I cannot seem to solve. It has to do with a custom field I'm attempting to add to a WooCommerce checkout page.

First, I've created a function to generate an array:

function eci_get_events() {

    $args = array(

        'post_type'         =>  'tribe_events',
        'posts_per_page'    =>  -1,
        'post_status'       =>  'publish',
        'order'             =>  'ASC',
        'orderby'           =>  'meta_value_num',
        'meta_key'          =>  '_EventStartDate'

    );

    $event_query = new WP_Query($args);

    $event_list = array();

    if ($event_query->have_posts()) : while ($event_query->have_posts()) : $event_query->the_post();

        $event_list[get_the_id()] = get_the_title();

    endwhile; endif; wp_reset_postdata();

    return $event_list;

}

Which is then used in this function:

function ecitpm_checkout_fields( $fields ) {

    $fields['billing']['eci_event'] = array(
        'type'          =>  'select',
        'label'         =>  __('Event', 'woocommerce'),
        'required'      =>  true,
        'class'         =>  array('form-row-wide'),
        'clear'         =>  true,
        'options'       =>  eci_get_events() // Here's that function
     );

    return $fields;

}
add_filter( 'woocommerce_checkout_fields', 'ecitpm_checkout_fields' );

No matter what I try, however, this thing does not render on the front end when I utilize the $event_query loop in the first function. For example:

Works:

$event_list = array();

$event_list[get_the_id()] = get_the_title();

Does not work:

$event_list = array();

if ($event_query->have_posts()) : while ($event_query->have_posts()) : $event_query->the_post();

    $event_list[get_the_id()] = get_the_title();

endwhile; endif; wp_reset_postdata();

Edit

As requested, here's a quick var_dump of the array produced by the eci_get_events() function:

array(2) {
  [11]=>
  string(11) "Hello There"
  [23]=>
  string(12) "Another Test"
}

I've tried just about everything, but haven't been able to find a solution.

Any ideas of how to solve this one?

Thanks! Thomas

TMcGee
  • 21
  • 2
  • Can you try `var_dump($event_query)` to see what you get from the query? Hard to know whether the problem is your query not returning what you think it should without having the same data as you. – helgatheviking Aug 17 '16 at 01:43
  • Hi @LoicTheAztec, using a secondary `WP_Query` in a plugin or elsewhere is perfectly fine. Plus I just installed the Events Calendar plugin, created 2 events, added the WooCommerce code to my theme and it works fine. I can't reproduce a problem. Here's a screenshot: http://d.pr/i/1dqU3 – helgatheviking Aug 17 '16 at 02:06
  • @helgatheviking I have never used the Events Calendar plugin yet… So you have much more expertise in here than me… – LoicTheAztec Aug 17 '16 at 02:11
  • I have about 10 more minutes of experience than you on this one. :) It just creates some custom post types with it's own brand of post meta, nothing out of the ordinary. – helgatheviking Aug 17 '16 at 02:15
  • The problem is that you are using a **specific commercial plugin** (accessible only to few people) and with special settings and behaviors. So practically nobody can help you in here, as is not possible to reproduce the same kind of issue you get. *So I will delete my answer soon.* – LoicTheAztec Aug 17 '16 at 21:12
  • Actually, the plugin is totally free (http://rtlyd.co/2bjtwLr) and the issue occurs regardless of the type of posts I'm trying to query. For example, if I remove the post meta key from the query and change `'post_type' => 'tribe_events'` to `'post_type' => 'post'` to issue persists. From what I gather, it seems to be the way the array is being produced that `$fields['billing']['eci_event']` does not seem to like. – TMcGee Aug 17 '16 at 21:36
  • 1
    I would try disabling all other plugins and switching to a default theme. As I've mentioned, your code works for me: it retrieves the two events I created and generates a select drop down with ID/event title pairs. – helgatheviking Aug 17 '16 at 23:18

0 Answers0