0
votes

I've added the ability to choose from custom post types in the woocommerce product admin tabs with a function as used in this tutorial http://www.remicorson.com/mastering-woocommerce-products-custom-fields/

so I've added a custom field

 woocommerce_wp_select(
   array(
     'id'      => '_circuit',
     'label'   => __( 'choose circuit', 'woocommerce' ),
     'options' => get_circuits_as_array()
         )
     );

now the function looks like this

function get_circuits_as_array(){
      $args = array( 'post_type' => 'top', 'posts_per_page' => -1,    'post_status'=>'published' );
      $loop = new WP_Query( $args );
      $circuits = array('0'=>'--wybierz opcję--');
      while ( $loop->have_posts() ) : $loop->the_post();
     setup_postdata( $post );
      $circuits[get_the_id()] = get_the_title();
      endwhile;
      wp_reset_query();
      return $circuits;
    }

The problem is that while uploading the code to the server this function breaks the variations window it shows only the default "add variations message"

enter image description here

The console shows no errors.

I guess this has something to do with ajax requests but cant figure out exactly what, I've tries to move the get function in other files etc. but no luck.

The woocommerce plugin version is 2.2.8

1

1 Answers

0
votes

OK so I've figured this out and the workaround is to use a foreach loop with $loop->posts as the array

function get_circuits_as_array(){
      $args = array( 'post_type' => 'top', 'posts_per_page' => -1, 'post_status'=>'published' );
      $loop = new WP_Query( $args );
      $circuits = array('0'=>'--wybierz opcję--');
      foreach ($loop->posts as $circuit) {
        $circuits[$circuit->ID] = $circuit->post_title;
      }
      wp_reset_query();
      return $circuits;
    }