How To Add Product In Woocommerce With Php Code

How To Articles

Today, I would like to present an extensive tutorial on utilizing PHP code to incorporate a product into WooCommerce. As a web developer, I have discovered this to be a valuable expertise that enables me to tailor and streamline the process of integrating products into an e-commerce website.

Before we dive into the PHP code, let’s start by understanding what WooCommerce is. WooCommerce is a popular plugin for WordPress that enables you to transform your website into a fully functional e-commerce store. With WooCommerce, you can sell physical and digital products, manage inventory, and process payments.

Adding a product in WooCommerce can be done manually through the WordPress admin dashboard. However, if you have a large number of products that need to be added, it can be much more efficient to use PHP code to automate the process.

The first step is to establish a connection with the WordPress database. You can achieve this by including the WordPress functions in your PHP file:

require_once( 'path/to/wordpress/wp-load.php' );

Next, you will need to create an instance of the WooCommerce product class:

$product = new WC_Product();

Now that we have our product object, we can start setting its properties. Here are some of the most commonly used properties:

  • $product->set_name( 'Product Name' ); – Sets the name of the product.
  • $product->set_description( 'Product Description' ); – Sets the description of the product.
  • $product->set_sku( 'Product SKU' ); – Sets the SKU (Stock Keeping Unit) of the product.
  • $product->set_regular_price( 9.99 ); – Sets the regular price of the product.
  • $product->set_sale_price( 7.99 ); – Sets the sale price of the product.
  • $product->set_stock_quantity( 10 ); – Sets the stock quantity of the product.

Once you have set all the desired properties of the product, you can save it using the following code:


This will create a new product in WooCommerce with all the specified properties. You can then retrieve the ID of the newly created product using:

$product_id = $product->get_id();

Now that we have successfully added a product programmatically using PHP code, we can take it a step further and add some personal touches. For example, we can dynamically generate the product name and description based on user input or data from an external source.

Additionally, we can implement error handling and validation to ensure that the product is being added correctly. This way, we can display meaningful error messages to the user if anything goes wrong.


Adding a product in WooCommerce using PHP code is a powerful technique that can save you time and effort, especially when dealing with a large number of products. By automating the process, you can easily customize and personalize your product listings, making your online store stand out.

Remember to always test your code thoroughly and handle any errors gracefully. With a solid understanding of PHP and WooCommerce, you can take full control of your e-commerce website and create a seamless shopping experience for your customers.