Quickly create many products in WooCommerce

Imagine your customer has asked you to make e-commerce with 500 products or even more and he or she is not able to create all those products.

You are paid to develop the structure and design and you don’t have the wish to create 500 products, because it would be boring and you would have nothing to learn, but in the same time, it’s not convenient to outsource this activity, so what to do in this case?

Surely who had to do this boring job has to know the product information and which images have to upload for each product. This means that in some way your customer has to provide this information.

So if the customer has to work to provide this information, why to don’t take advantage of that work to create the products?

Here comes the trick.

As you probably know WooCommerce gives you the possibility to upload and export products with a CSV file.

So do the following:

  • create one product
  • export a CSV file including that product
  • copy the CSV file content and paste it into a Google Sheet  that you can share with your customer
  • ask your customer to fill the Google Sheet with the products information
  • ask your customer to provide the product pictures uploading them into the Media Library, but being very careful to give to the pictures the same titles of the related products
  • after the Google Sheet is filled and the Media Library has all the pictures, copy its content and paste it on the uploadable CSV
  • upload the CSV

At this point, you should have all the products, but still without featured image. Estimated time to do all that described above: 30 minutes. This is the time you need, of course, your customer will need a lot more to fill the Google Sheet, but you can’t help him to do that, because you don’t have the products information and you don’t have the product pictures, at least in a normal situation.

Now the last effort you need to do is to assign every image to the right product.

If your customer had titled the images as the related products as you asked, (this is really the most difficult part of the process), you can run a PHP script to do the remaining job for you.

So let’s see how we can write a few lines of code to reach our goal.

You should always be very careful about security when you write code, but in this case, let’s be a little quick and dirty.

Be just careful to don’t forget to leave the following code after you have got what you wanted.

You can temporarily put the following snippet in your functions.php file.

if( isset( $_GET['ump'] ) && $_GET['upm'] === '12344321' ){
	$products = get_posts( array( 'post_type' => 'product','posts_per_page' => -1 ) );
	$attachments = get_posts( array( 'post_type' => 'attachment','posts_per_page' => -1 ) );
	foreach( $products as $product ){
		if( '' === get_post_thumbnail_id( $product->ID ) ){
			$product_name = esc_attr( strtolower( $product->post_title ) );
			foreach( $attachments as $attachment ){
				$attachment_name = esc_attr( strtolower( $attachment->post_title ) );
				if( $product_name === $attachment_name ){
					set_post_thumbnail( $product,$attachment->ID );
				}	
			}
		}
	}
}

It’s ok sometimes to be quick and dirty but to don’t exaggerate  we use the condition

if( isset( $_GET['ump'] ) && $_GET['upm'] === '12344321' ){

}

So the code that will assign the images to the products will run only if in the URL we add the query argument ?upm=12344321
This means that if your site is reachable at https://your-site.com, the code will only run if you visit your site at https://your-site.com?upm=12344321.
Doing so we add a little level of security and avoid that everybody can make run the script.
Of course, if you want, you can add other conditions, e.g. checking if the current user can manage options if the visited page is in the back-end and whatever you consider safer.

The script is self-explanatory.
We first create the object $products that contain all the products.
We do the same for the images with the object $attachments.

Then we make run a cycle for each product included in the object $products.
For each product, if it hadn’t a featured image, we make run a cycle for each image and we check if the image title is equal to the product title. If so, we assign that image to the product.
As you have probably noticed, we use the functions esc_attr and strtolower on both the images and products titles. This is to avoid to miss an image only because its title is e.g. uppercase and the product title lowercase, or maybe the product tile has a “!” and the image title not. If you want you could even add a regex filter to increase the number of differences between the images and products titles.

After you have checked that the code worked, remove it. So your functions.php will be again clean and safe as before.