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.
- Open your child theme’s
functions.phpfile. - 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.
- Add the following code to your
functions.phpfile 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
- Log in to your WordPress admin dashboard.
- Navigate to
Productsand check if theSample Productis listed. - Verify that the product details, such as price, SKU, stock status, and categories, are correctly set.
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.
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.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch


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