Insert new woocommerce product programmatically

How to insert brand new Woocommerce product programmatically? Sometimes you need your own product importer. You may read database, XML, JSON, CSV, XLS, XLSX, ODS, absolutely anything. Now you got all your products in memory or cache or your temporary file and what’s next? You need to add this product to Woocommerce…

Well, the product is – like almost anything in WordPress – post type with specific taxonomy and meta data set. So, first things first, insert your new post:

Of course this is not everything, that would be too easy. Now we need to add some meta information to the product post and that is why, we take care of our new posts ID.
Now we need to relate our post, to proper term and taxonomy. The function we need is wp_set_object_terms and we will use it like this:

This will relate our post as a simple product. Please be careful, because this function will create term/taxonomy if those doesn’t exists. First you must know you are running WordPress with Woocommerce enabled.
FiannlyFinally, we need to setup some meta informations. Some of them are important, some of them doesn’t, but it is always a good practice to set it up. We need to use update_post_meta function. There is no built-in function to do it with one call, so be prepared… Three, two, one:

And that’s it. You have a brand new product.

In practice. Of course I do realize it isn’t that easy in practice. Probably you will need a loop to insert much more products than one. Then you need to use some fields from that array… Let’s say we’ve got an array like that:

and additionally we let user to choose – what user (some other one) will be responsible for import. Then we need to do something like this:

That should work.

One last word about product importing. This is a bit time-consuming. I wrote two importers in a last week, I know what I’m talking about. They work like a charm, but I needed to use AJAX. Thanks to that, I was able to import as many products as I want (then I split the list to not exceed time execution limit) and – as a second bird with one stone – I got nice progress bar at the frontend, so WP admin was able to see the exact process of importing.

29 Comments

  1. I’m building new web site with web shop, it is not published yet. I need to make custom importer, Is there any chance to share one of yours importers?
    Thank You
    Best Regards

  2. Hi ,I added the products from quickbook pos application to woocommerce.Everything was updated successfully.But there is a problem on sales dates.I added the code as below
    update_post_meta( 8, ‘_sale_price_dates_from’, 1473638400 );
    update_post_meta( 8, ‘_sale_price_dates_to’, 1473638400 );
    Its a future timestamp .But its showing the products always on sale .Any help ??

    • Hi, there are many reasons why this may not work. First thought – please install wp debug cron to see if there are any planned tasks and deactivate/activate woocommerce. It’s a bit complicated to answer unseen problems 🙂

  3. I am trying to assign the category to product using wp_set_object_terms method, but it return the error with invalid taxonomy, I tried in every way to make it done, but not working,

    Any help would be apprecialble

  4. Great article! Exactly what I needed to turn member input into products with admin approval. In case anyone is wondering, you can add the product short description using: post_excerpt in the wp_insert_post function. Cheers!

  5. Thanks for the hints.
    I use my own API (link) to run commands against wordpress/woocommerce.
    That way I never run into problem with max exicution times -> no Ajax needed.
    All my importers run local (Mysql/PHP) whereas the shop runs on a remote server.

  6. Very nice and helpful article.

    I would like to ask if I am going to add a multiple insert using your code?

    Is there a function in wordpress that can handle multiple insert?

  7. Hi, Sorry to ask a basic question. I am a javascript developer just getting to grips with WordPress. I have a wordpress website that I want to create a product from an event on the form. I can call jQuery.ajax or equiv and I know the parmeters to pass.
    How do I call a PHP file that has access to the WC functions ? Sorry I know this is a very basic question, but I am learning PHP and WordPress at the same time.

    • Paul, it’s not that basic, but it also isn’t very hard.
      The right way to do ajax calls in WordPress environment is this:
      – first, you need a place to store your PHP code. You may write a plugin or use functions.php in your theme (of course you will need to use child-theme if it isn’t your theme)
      – then, you need to hook at least one of those actions:
      wp_ajax_something and wp_ajax_nopriv_something
      First is used for logged users (wp_ajax_something) and second is used for non-logged users (wp_ajax_nopriv_something).
      I assume that only logged in user may be able to add product, so:
      add_action( ‘wp_ajax_pauladdproduct’, ‘pauladdproduct’ );
      and then:
      function pauladdproduct() {
      # here you are adding a product
      }
      To do a proper ajax call, you will need something like this:
      data = { action: ‘pauladdproduct’, value1: ‘some value’, ‘value2’: ‘another value’ };
      jQuery.post( ajaxurl, data, function(response){
      alert(response);
      });
      This should work.

  8. Hi Łukasz, thanks for sharing such a good and simple tutorial. Its working fine for me, can you tell us also about how to add product images and feature image.

    • Luciano, variable products are, in simple terms, other post (children of product post) with specific title, IDs etc. You may find examples over the internet, for sure. Maybe I will write about it. Comment is much too small to write more. But it is similar job. Regards!

      • I’ll really appreciate a code sample by you. I’ve found the way to add variations but they are not listed at frontend, just an empty select for my variations despite they are listed in admin. I think I’m missing something here like saving variation attributes. Regards!

  9. Thank you Łukasz for this awesome tutorial. I can now import my products from my custom xml file to the woocommerce product page. Actually I am building an importer plugin and I have one more question here. It would be really great if you can post a sample code on how to import attributes as well. Your examples are more understandable than those in the internet. Regards!

  10. Hi Łukasz Nowicki,

    Thanks for the code. I am building a custom product importer, do you know something from which I can upload .xlxs or .xls file instead of .csv any library or something?

    Please let me know.

Leave a Reply

Your email address will not be published.


*