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:
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