Python ve Wordpress'e hakim bir developer ihtiyacım var.
2
●98
- 11-01-2024, 20:19:35b13 adlı üyeden alıntı: mesajı görüntüle
import requests import base64 import os # WooCommerce API bilgileri url = 'https://your-woocommerce-store.com/wp-json/wc/v3/products' consumer_key = 'your_consumer_key' consumer_secret = 'your_consumer_secret' # Ürün verileri product_data = { "name": "Sample Product", "type": "simple", "regular_price": "19.99", "description": "This is a sample product description.", "short_description": "Sample Short Description", "categories": [ {"id": 1} # Kategori ID'sini ayarlayın ] } # Ana görsel dosyasının yolu main_image_path = 'path/to/main_image.jpg' # Galeri resimlerinin bulunduğu klasör gallery_folder_path = 'path/to/gallery/' # Ana görseli base64'e çevirip ekle with open(main_image_path, "rb") as image_file: main_image_base64 = base64.b64encode(image_file.read()).decode('utf-8') product_data["images"] = [{"src": f'data:image/jpeg;base64,{main_image_base64}'}] # Ürünü ekleyin response = requests.post( url, auth=(consumer_key, consumer_secret), json={"product": product_data} ) # Eklenen ürünün ID'sini alın product_id = response.json().get('id') # Galeri resimlerini ekleyin for filename in os.listdir(gallery_folder_path): image_path = os.path.join(gallery_folder_path, filename) with open(image_path, "rb") as image_file: image_base64 = base64.b64encode(image_file.read()).decode('utf-8') # Galeri resimini ekleyin requests.post( f'{url}/{product_id}/gallery', auth=(consumer_key, consumer_secret), json={"src": f'data:image/jpeg;base64,{image_base64}'} ) print("Ürün ve galeri resimleri başarıyla eklendi.")