WP All Import makes WooCommerce products invisible

I was working on a WooThemes customer ticket today and he was having an issue with the products he bulk imported into his WordPress install with a plugin called WP All Import. All his products were showing fine in the WordPress dashboard but those products were not showing on the frontend.

[box type=”green”]EDIT: the issue has been fixed by WP All Import authors! yeah![/box]

The reason is that many meta fields were missing in wp_postmeta, in the database.

So I created a small plugin to solve the issue. The snippet provided below is a custom plugin that will create a new field called _visibility, and it will set the value of this meta field to visible. That’s it. Of course you could improve this plugin and create all missing fields, but this field is enough for WooCommerce to make the products display on the site. So, if you have this specific issue I hope that this will help you ;-)

<?php
/*
Plugin Name: WooCommerce - Create missing visible field
Plugin URL: http://remicorson.com/
Description: Create missing visible field
Version: 0.1
Author: Remi Corson
Author URI: http://remicorson.com
Contributors: corsonr
Text Domain: none
Domain Path: languages
*/

add_action( 'init' , 'wc_create_missing_field' );

/**
 * Create Missing fields in WC
 *
 * @access      public
 * @since       1.0
 * @return      void
*/
function wc_create_missing_field() {

	global $wpdb;

	$post_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE 'post_type' = 'product'");

	foreach( $post_ids as $post_id ) {
		$wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '_visible' WHERE meta_key = '_visibility' AND post_id = '$post_id'");
	}

}