I have hundreds of art images in my WP media library, each of which I want to convert into a woocommerce product, and set as featured image of that product.
Rather than manually adding new products and setting featured images, which would take days of repetitive work, is there a way to do this automatically in MySQL?
I’d agree that doing this in SQL would be a lot more to learn than PHP. With WordPress functions you can run a query on all the images (
WP_Query
) and then loop through the results and use the info from the images to create a new post using[wp_insert_post()][2]
. Finally you can update that post’s_thumbnail_id
meta key ([update_post_meta()][3]
, which stores the ID of the featured image.Totally untested:
You would only want to run this ONCE, or you will get a small ton of new products.
Editing to add that you can ensure that this only runs one time with the use of a transient.
You want to create Woocommerce products, and to do a good job you don’t need only the post object and the thumbnail: you need taxonomies and custom fields, too: I think your product will have – at least – a price, isn’t it?
Once you want to bulk create products I suppose that having same taxonomies and same custom fields for all of them is good for you (anyway manually changes are always possible).
I will suggest an approach, and give related code, but note is all not tested.
First of all, manually create one product: it will be your reference product.
Set everything for it: prices, variation, taxonomies… everything. Set also the thumbnail, off course.
Now just take note of the post ID of this product.
Second step is create a function that loops through your attachments and creates posts, assigns thumbnails, custom fields and taxonomies.
ADVICE: please note that woocommerce use some custom tables to save some informations, my method ignore this tables, so you will not bulk assign to posts information/settings about downloadable products. If you want create downloadable products from images you have to modify function before use it. Moreover some WooCommerce plugin use custom tables, too: be aware of this before running the function.
…Yes, it’s a monster function…
Third and last step is the easiest one, you have to call the function just created and pass the reference product id as param. There are many way to do this task, here just one:
Note that normally the function will create products for all your images. I added a second param to
wpa_convert_images_to_products
function that can be an array of images id you want to skip.Another way to create products from images selectively is create a custom taxonomy for attachments (info here) and then use appropriate
tax_query
in the images WP_Query to select which images to convert.Hope it helps.
No. There is no command in MySQL that can convert an entry to a woocommerce product specific format. The reason is that the database doesn’t “know” that format.
There is a command language to do that. You can use an SQL query on the database to change the database directly (without using PHP).
It is sometimes easier to query the database directly to manipulate large amounts of data. That is one reason for using a database in the first place. Ultimately, that is what Woo Commerce is doing in PHP.
Given that you are asking this question, it is unlikely that you already possess the knowledge to write this query without a (perhaps steep) learning curve. If it is really going to take a long time to convert the products by hand, pay a professional who is familiar with the format to covert the data for you. The time you save may be worth the cost. Contacting the Woo Commerce authors might be a good resource for finding that person.
If you have some time and you want to do this yourself, the first thing to do is to find out what the Woo Commerce format is for product storage.