import requests
import csv
import os
# Credentials from sync_inventory.py
WC_USERNAME = "justinhenshaw03@gmail.com"
WC_PASSWORD = "zrzV 4GEe Cph4 2gU0 5mub BbCW"
WC_URL = "https://seedvault.market/wp-json/wc/v3/products"
OUTPUT_FILE = "wc_products_export.csv"
def fetch_all_wc_products():
all_products = []
page = 1
per_page = 100 # Max allowed by WooCommerce
print("Fetching products from WooCommerce (paginating)...")
while True:
print(f" Fetching page {page}...")
params = {
'page': page,
'per_page': per_page
}
response = requests.get(WC_URL, auth=(WC_USERNAME, WC_PASSWORD), params=params)
if response.status_code != 200:
print(f"Error fetching page {page}: {response.status_code} - {response.text}")
break
products = response.json()
if not products:
break
all_products.extend(products)
page += 1
return all_products
def export_to_csv(products):
if not products:
print("No products found to export.")
return
fieldnames = ['name', 'sku', 'id', 'description', 'regular_price']
try:
with open(OUTPUT_FILE, mode='w', newline='', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, extrasaction='ignore')
writer.writeheader()
for p in products:
# Clean up description (remove HTML tags for the CSV)
desc = p.get('description', '')
import re
clean_desc = re.sub('<[^<]+?>', '', desc)
writer.writerow({
'name': p.get('name'),
'sku': p.get('sku'),
'id': p.get('id'),
'description': clean_desc,
'regular_price': p.get('regular_price')
})
print(f"\nSuccessfully exported {len(products)} products to {OUTPUT_FILE}")
except Exception as e:
print(f"Error writing CSV: {e}")
if __name__ == "__main__":
products = fetch_all_wc_products()
export_to_csv(products)