Integrating WooCommerce with Your Existing Web App
To integrate your existing web app with WooCommerce so you can push product data from your app to WooCommerce, you have several options:
Option 1: WooCommerce REST API (Recommended)
The most flexible and maintainable approach is using WooCommerce’s REST API:
- Enable the REST API in WooCommerce:
- Go to WooCommerce → Settings → Advanced → REST API
- Click “Add Key”
- Generate API keys (Consumer Key and Consumer Secret)
- Push products from your app:
/**
* WooCommerce Product Creation Example
* Using the WooCommerce REST API in Node.js
*/
// Import the WooCommerce REST API client
const WooCommerceRestApi = require("@woocommerce/woocommerce-rest-api").default;
// Configure the API client
const api = new WooCommerceRestApi({
url: "https://your-woocommerce-site.com", // Your store URL
consumerKey: "ck_your_consumer_key", // Your consumer key
consumerSecret: "cs_your_consumer_secret", // Your consumer secret
version: "wc/v3" // API version
});
// Product data to be created
const productData = {
name: "Premium Wool Sweater", // Product name
type: "simple", // Product type
regular_price: "59.99", // Base price
description: "Warm, high-quality wool sweater for winter.", // Full description
short_description: "Winter wool sweater", // Short description
categories: [ // Product categories
{
id: 9 // Category ID
}
],
images: [ // Product images
{
src: "http://example.com/sweater.jpg", // Image URL
alt: "Premium Wool Sweater" // Alt text
}
]
};
/**
* Create the product using the WooCommerce API
*/
api.post("products", productData)
.then(response => {
// Success handler
console.log("✅ Product created successfully!");
console.log("Product ID:", response.data.id);
console.log("View product:", response.data.permalink);
})
.catch(error => {
// Error handler
console.error("❌ Error creating product:");
console.error("Status:", error.response.status);
console.error("Message:", error.response.data.message);
if (error.response.data.details) {
console.error("Details:", error.response.data.details);
}
});
Option 2: WordPress XML-RPC API
For simpler implementations, you can use WordPress’s built-in XML-RPC API:
// Example PHP code to create product via XML-RPC
require_once('IXR_Library.php');
$client = new IXR_Client('http://yoursite.com/xmlrpc.php');
$client->query('wp.newPost',
1, // blog_id (usually 1)
'username',
'password',
array(
'post_type' => 'product',
'post_title' => 'New Product',
'post_content' => 'Product description',
'post_status' => 'publish'
)
);
Option 3: Direct Database Access (Not Recommended)
While possible, directly modifying WooCommerce’s database tables is risky and not recommended for production environments.
Implementation Steps
- Set up authentication between your app and WooCommerce
- Create product data structure in your app that maps to WooCommerce fields
- Implement synchronization logic:
- Decide when to push updates (real-time, scheduled batches)
- Handle updates vs. new products
- Manage inventory and variations if needed
- Add error handling for failed syncs
- Test thoroughly before going live
Additional Considerations
- Product visibility: Products will be visible on the frontend immediately after creation unless you set status: “draft” and publish later
- Images: You’ll need to upload images to WordPress media library first
- Performance: For large catalogs, consider batch processing
- Webhooks: Set up WooCommerce webhooks if you need your app to react to store events
