How to Create a Product Programmatically in WooCommerce – 2026

Last updated on August 14th, 2024 at 06:00 am

Creating products programmatically in WooCommerce can be a powerful tool for store owners and developers. Whether you’re looking to automate product uploads, integrate with third-party systems, or simply streamline your workflow, this guide will show you how to add products to your WooCommerce store programmatically.

Why Create Products Programmatically?

Creating products programmatically can save time and reduce errors, especially if you’re dealing with large inventories or frequent updates. It allows you to integrate WooCommerce with other systems, automate product management tasks, and ensure consistency across your product catalog.

Step 1: Set Up Your Environment

Before you start, make sure you have a child theme set up. If you don’t have one, you can use our free child theme generator.

  1. Open your child theme’s functions.php file.
  2. Add the following code to define a function that creates a product:
/*
 * Snippet: How to Create a Product Programmatically in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1366
* Tested with WooCommerce 10.7.0
* "Function to create a product programmatically"
*/ function wcsuccess_create_product() { $post_id = wp_insert_post(array( 'post_title' => 'Sample Product', 'post_content' => 'This is a sample product created programmatically.', 'post_status' => 'publish', 'post_type' => 'product', )); if ($post_id) { // Set product type wp_set_object_terms($post_id, 'simple', 'product_type'); // Set product price update_post_meta($post_id, '_regular_price', '19.99'); update_post_meta($post_id, '_price', '19.99'); // Set product SKU update_post_meta($post_id, '_sku', 'sample-product-sku'); // Set stock status update_post_meta($post_id, '_stock_status', 'instock'); // Set stock quantity update_post_meta($post_id, '_stock', '100'); // Set product visibility update_post_meta($post_id, '_visibility', 'visible'); // Add product image (optional) $image_id = attachment_url_to_postid('https://yourwebsite.com/path/to/image.jpg'); set_post_thumbnail($post_id, $image_id); // Add product categories (optional) wp_set_object_terms($post_id, array('Category 1', 'Category 2'), 'product_cat'); } }

Step 2: Execute the Function

To run the function and create the product, you can call the function directly or hook it into an action. Here, we’ll demonstrate how to hook it into the init action for testing purposes. Be sure to remove or comment out the hook after you’ve created your product to avoid creating duplicate products.

  1. Add the following code to your functions.php file to execute the function:
/*
 * Snippet: How to Create a Product Programmatically in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1366
* Tested with WooCommerce 10.7.0
* "Hook the function into init action"
*/ add_action('init', 'wcsuccess_create_product');

Step 3: Verify the Product

  1. Log in to your WordPress admin dashboard.
  2. Navigate to Products and check if the Sample Product is listed.
  3. Verify that the product details, such as price, SKU, stock status, and categories, are correctly set.
See also  How to Bulk Increase WooCommerce Prices Across the Site 2026

Optional – Passing Attributes

To pass attributes such as the title and price to the function, you can modify the function to accept parameters. This way, you can call the function with different values for each product. Here’s how you can adjust the function to accept parameters:

/*
 * Snippet: How to Create a Product Programmatically in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1366
* Tested with WooCommerce 10.7.0
* "Function to create a product programmatically with parameters"
*/ function wcsuccess_create_product($title, $content, $price, $sku, $stock, $image_url, $categories) { $post_id = wp_insert_post(array( 'post_title' => $title, 'post_content' => $content, 'post_status' => 'publish', 'post_type' => 'product', )); if ($post_id) { // Set product type wp_set_object_terms($post_id, 'simple', 'product_type'); // Set product price update_post_meta($post_id, '_regular_price', $price); update_post_meta($post_id, '_price', $price); // Set product SKU update_post_meta($post_id, '_sku', $sku); // Set stock status update_post_meta($post_id, '_stock_status', 'instock'); // Set stock quantity update_post_meta($post_id, '_stock', $stock); // Set product visibility update_post_meta($post_id, '_visibility', 'visible'); // Add product image (optional) if ($image_url) { $image_id = attachment_url_to_postid($image_url); set_post_thumbnail($post_id, $image_id); } // Add product categories (optional) if ($categories) { wp_set_object_terms($post_id, $categories, 'product_cat'); } } } // Example usage $title = 'Dynamic Product Title'; $content = 'This is a dynamically created product.'; $price = '29.99'; $sku = 'dynamic-product-sku'; $stock = '50'; $image_url = 'https://yourwebsite.com/path/to/image.jpg'; $categories = array('Category 1', 'Category 2'); /* * * Snippet: How to Create a Product Programmatically in WooCommerce – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1366
* Tested with WooCommerce 10.7.0
* "Hook the function into init action with parameters"
*/ add_action('init', function() use ($title, $content, $price, $sku, $stock, $image_url, $categories) { wcsuccess_create_product($title, $content, $price, $sku, $stock, $image_url, $categories); });

Benefits of Using Parameters

  • Flexibility: Easily create multiple products with different attributes without modifying the function each time.
  • Integration: Integrate with other systems or scripts that can dynamically provide product attributes.
  • Automation: Automate bulk product creation or updates with varying attributes.
See also  How to Set Maximum Order Quantity Based on User Role in WooCommerce - 2026

This approach allows you to dynamically create products with custom attributes, making your WooCommerce store management more flexible and efficient.

Use Cases

Bulk Product Imports

If you need to import a large number of products from a CSV file or another source, you can modify the function to loop through the data and create products in bulk.

Integration with External Systems

Integrate WooCommerce with third-party systems, such as inventory management or ERP systems, to automate product creation and updates based on external data.

Custom Workflows

Automate specific workflows, such as creating a new product when certain conditions are met, to streamline your product management process.

Conclusion

Creating products programmatically in WooCommerce is a powerful way to automate and streamline your product management tasks. By following the steps outlined above, you can easily add products to your WooCommerce store programmatically, saving time and ensuring consistency.

For more customization tips and tools, don’t forget to check out our free WordPress child theme generator.

4.5 2 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jim Camomile
October 23, 2024 8:22 am

Wow, this is excellent. Very clear and not a lot of second level, confusing details. Thanks!!

Scroll to Top
1
0
Would love your thoughts, please comment.x
()
x
×