Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shayanabbas/f20032a9efd2a4f15d351a47857551d9 to your computer and use it in GitHub Desktop.
Save shayanabbas/f20032a9efd2a4f15d351a47857551d9 to your computer and use it in GitHub Desktop.
Import Products to WooCommerce using WordPress REST API

You can use the built-in WordPress REST API to import products into WooCommerce. WooCommerce extends the WordPress REST API with its own specific endpoints, allowing you to create, read, update, and delete products programmatically. Here's how you can do this:

Enable the WordPress REST API: Ensure that the WordPress REST API is enabled on your site. It is enabled by default in recent WordPress versions.

Authentication: To use the REST API, especially for writing data such as importing products, you'll need to authenticate. You can use one of the following methods:

Basic Authentication: This is simpler for development purposes but not recommended for production due to security concerns unless used over HTTPS. OAuth Authentication: More secure and suitable for production environments. Understand WooCommerce REST API Endpoints: WooCommerce adds several endpoints to the WordPress REST API for handling products. The endpoint for products typically looks like this: /wp-json/wc/v3/products.

Prepare Your Data: Format the product data you want to import in a structure compatible with the WooCommerce REST API. This will usually be in JSON format, including fields like name, type, regular price, description, categories, images, etc. ** Create a Product:** To create a product, you'll make a POST request to the /wp-json/wc/v3/products endpoint with your product data. Here's a basic example in curl:

curl -X POST https://yourdomain.com/wp-json/wc/v3/products \
     -u consumer_key:consumer_secret \
     -H "Content-Type: application/json" \
     -d '{
           "name": "Product Name",
           "type": "simple",
           "regular_price": "19.99",
           "description": "Product Description",
           "short_description": "Short Description",
           "categories": [
             {
               "id": 22
             }
           ],
           "images": [
             {
               "src": "http://example.com/wp-content/uploads/product.jpg"
             }
           ]
         }'

Handling Bulk Import: For bulk imports, you'll need to create a script that iterates through your list of products and sends a POST request for each one. This can be done using any programming language that can make HTTP requests and handle JSON data, like Python, PHP, JavaScript, etc.

Rate Limiting and Execution Time: Be aware of rate limiting on the WooCommerce REST API and execution time limits on your server. You may need to implement pauses in your script to avoid hitting these limits.

Testing: Initially, test with a few products in a staging or development environment before going live.

Permissions and Security: Make sure the user whose credentials you are using has the appropriate permissions to create products.

Error Handling: Implement error handling in your script to deal with any issues that arise during the import process.

This method requires some programming knowledge and understanding of RESTful APIs. If you're not comfortable with coding, you might want to consider the plugin approach or seek assistance from a developer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment