This guide will show you how to create a WooCommerce product programmatically. This is intended for developers or people comfortable to work with code.

WooCommerce uses a Custom Post Type to create and store products, we’ll create a simple product via PHP and set a price and sale price for it. This is a starting point of creating WooCommerce products programmatically and can be extended on further to build your own solutions.

You may use the following code to your site or project to create a project via PHP:

You may view https://woocommerce.github.io/code-reference/classes/WC-Product.html for all the available methods to use to update the WooCommerce meta data. Look for methods with the ‘set_’ prefix.
<?php
/**
  * Sample function to create a WooCommerce product programmatically using PHP.
  * This code will insert the product into the WordPress post's table and then update the WooCommerce pricing/settings.
  * @see https://woocommerce.github.io/code-reference/classes/WC-Product.html for available methods.
  *
  * Please note this is a _starting point_ and would need to be 'hooked' into a relevant action such as form submission, etc.
  * We recommend reaching out to a developer for assistance with this code if it's unclear. Replace values where you see fit.
  */
 function my_create_woo_product( $data = null ) {
    $post_args = array(
        'post_author' => intval( $data['author_id'] ), // The user's ID
        'post_title' => sanitize_text_field( $data['title'] ), // The product's Title
        'post_type' => 'product',
        'post_status' => 'publish' // This could also be $data['status'];
    );

    $post_id = wp_insert_post( $post_args );
   
    // If the post was created okay, let's try update the WooCommerce values.
    if ( ! empty( $post_id ) && function_exists( 'wc_get_product' ) ) {
        $product = wc_get_product( $post_id );
        $product->set_sku( 'pre-' . $post_id ); // Generate a SKU with a prefix. (i.e. 'pre-123') 
        $product->set_regular_price( '20.55' ); // Be sure to use the correct decimal price.
        $product->set_category_ids( array( 16, 17 ) ); // Set multiple category ID's.
        $product->save(); // Save/update the WooCommerce order object.
    }
}

In the above code, the process will be to insert the product with a title into the WordPress database. Once the post is created correctly we then set the product meta such as the price, SKU, assign it to categories and more.

If you need to update an existing product, you will skip the wp_insert_post code and reference the product’s ID with the last part of the code (lines 21-27).

This code snippet is a starting point and will need some work done to get this working on your WooCommerce site. We recommend reaching out to a developer if you are struggling to get this working or leave a comment below.

If you found this article useful and would like to receive updates when new articles like this are published – join our newsletter by filling out your information below.

Double opt-in required and easily opt-out anytime. We promise to never spam you!

Photo by Joseph Barrientos on Unsplash

Stay in the loop!

15% off your next purchase, just for you 🎁

Sign up to receive your exclusive discount, and keep up to date on our latest news, products & offers!

We don’t spam, ever! Read our privacy policy for more info.