How I Built an Automated XML Product Feed with AI Assistance
Recently, I needed to build an automated XML product feed for a WooCommerce store that would be compatible with a third-party marketplace. The task required generating an XML file that contained all product data, validated against specific rules, and updated daily to reflect changes in pricing, stock, and images. Here’s how I approached the problem using AI, Python, and WooCommerce’s API.
Tools and Technologies Used:
- WooCommerce API: To retrieve product data, including details like prices, descriptions, stock statuses, and images.
- Python: For writing scripts that generate the XML file and handle image conversions.
- Pillow (Python Imaging Library): To convert product images from WebP to JPG format.
- Virtual Environment (Python venv): To manage dependencies on a Linux server.
- Linux Terminal & SSH: For server management and deployment tasks.
- AI (ChatGPT): For guidance on code optimization, troubleshooting errors, and ensuring XML structure validation.
Step-by-Step Process
1. Connecting to the WooCommerce API
The first step was to fetch product data from WooCommerce. I used the WooCommerce REST API, which provided all the necessary data for each product, such as product ID, name, price, SKU, stock status, and image URLs.
Example API connection:
import requests
url = “https://your-site.com/wp-json/wc/v3/products”
consumer_key = “your_consumer_key”
consumer_secret = “your_consumer_secret”
response = requests.get(url, auth=(consumer_key, consumer_secret))
products = response.json()
This API call retrieves all product data, which can then be used to populate the XML feed.
2. Generating the XML Feed
Next, I wrote a Python script to generate the XML file. Each product was represented as an <item>
element in the XML, containing the required fields like product name, ID, price, stock status, and image URLs. The XML file had to follow the specific structure required by the third-party marketplace.
Example XML generation code:
xml_data = '<?xml version="1.0" encoding="UTF-8"?>'
xml_data += '<items>'
for product in products:
xml_data += '<item>'
xml_data += f'<item_id>{product["id"]}</item_id>'
xml_data += f'<item_name>{product["name"]}</item_name>'
xml_data += f'<start_price>{product["sale_price"]}</start_price>'
xml_data += f'<stock_status>{product["stock_status"]}</stock_status>'
xml_data += '</item>'
xml_data += '</items>'
This code generates a basic XML structure for each product, with dynamic data being fetched directly from the WooCommerce API response.
3. Handling Image Conversion (WebP to JPG)
One challenge was that the third-party platform required JPG images, but many of my product images were in WebP format. To handle this, I used the Python Pillow
library to convert WebP images to JPG before including them in the XML feed.
WebP to JPG conversion using Pillow:
from PIL import Image
import os
image_directory = “/path/to/webp/images”output_directory = “/path/to/jpg/images”
for filename in os.listdir(image_directory):
if filename.endswith(“.webp”):
with Image.open(os.path.join(image_directory, filename)) as img:
jpg_filename = filename.replace(“.webp”, “.jpg”)
img.convert(“RGB”).save(os.path.join(output_directory, jpg_filename), “JPEG”)
This script checks for WebP images, converts them to JPG, and saves them in the specified directory. These converted images were then linked in the XML feed.
4. Automating Daily Updates
To ensure the XML feed was always up-to-date with the latest product changes (like price, stock status, and new products), I automated the process by integrating the XML generation script with WordPress. I scheduled the script to run daily using a custom WooCommerce plugin.
Example of scheduling the task in WordPress:
if (!wp_next_scheduled('generate_daily_product_feed_xml')) {
wp_schedule_event(time(), 'daily', 'generate_daily_product_feed_xml');
}
add_action(‘generate_daily_product_feed_xml’, ‘generate_product_feed_xml’);
This PHP code schedules the XML generation task to run every day, ensuring the feed is updated automatically.
5. Validating and Troubleshooting the XML
Using AI assistance (ChatGPT), I was able to resolve several validation errors in the XML structure. These errors included issues like the incorrect order of elements or missing mandatory fields. With AI guidance, I quickly identified the problems and adjusted the XML structure accordingly.
Example of solving an XML validation issue: AI helped me realize that the <quantity>
tag must appear after the <date_start>
tag in the XML schema, which was critical for the XML validation to pass.
6. Deploying the XML Feed
Once the XML file was validated and ready, I deployed it to the server and made it accessible at a fixed URL (e.g., https://your-site.com/xml/product_feed.xml
). This URL was shared with the third-party platform, where they could fetch the updated product feed daily.
Key Takeaways
- AI as a Coding Assistant: Leveraging AI tools like ChatGPT significantly sped up problem-solving, especially when dealing with XML validation and optimizing the code.
- Automation Saves Time: Automating the daily generation of the XML feed ensured that all product updates (price, stock, new products) were reflected without manual intervention.
- Handling Image Formats: The challenge of image format compatibility was solved by integrating the Pillow library to convert WebP to JPG seamlessly.
- WooCommerce Integration: Using the WooCommerce API made it easy to fetch product data and integrate it into the XML structure required by the third-party marketplace.
This project demonstrated how modern tools, including AI, can be leveraged to automate and simplify traditionally time-consuming tasks. If you’re looking to streamline product feed generation or similar workflows, I highly recommend using a combination of APIs, Python scripting, and AI-powered assistance.