حذف قیمت تمام محصولات ووکامرس با API ووکامرس + با پایتون
در این نوشته با کمک woocommerce api قیمت تمام محصولات را حذف میکنیم.
حذف قیمت تمام محصولات ووکامرس با API ووکامرس
برای حذف قیمت تمام محصولات ووکامرس با کمک ایپیآی ووکامرس از قطعه کد زیر در پایتون استفاده کنید:
import requests BASE_URL = "https://test.com/wp-json/wc/v3" CONSUMER_KEY = "ck_111111111111111111111111111111111111111111" CONSUMER_SECRET = "cs_111111111111111111111111111111111111111111" def get_all_products(): products = [] page = 1 while True: url = f"{BASE_URL}/products?page={page}&per_page=100" response = requests.get(url, auth=(CONSUMER_KEY, CONSUMER_SECRET)) if response.status_code != 200: print(f"Failed to retrieve products: {response.json()}") break page_data = response.json() if not page_data: break products.extend([product['id'] for product in page_data]) page += 1 return products def get_product_variations(product_id): variations = [] page = 1 while True: url = f"{BASE_URL}/products/{product_id}/variations?page={page}&per_page=100" response = requests.get(url, auth=(CONSUMER_KEY, CONSUMER_SECRET)) if response.status_code != 200: print(f"Failed to retrieve variations for product ID {product_id}: {response.json()}") break page_data = response.json() if not page_data: break variations.extend([variation['id'] for variation in page_data]) page += 1 return variations def clear_product_prices(product_id): url = f"{BASE_URL}/products/{product_id}" data = { "regular_price": "", "sale_price": "" } response = requests.put(url, json=data, auth=(CONSUMER_KEY, CONSUMER_SECRET)) if response.status_code == 200: print(f"Successfully cleared prices for product ID {product_id}") else: print(f"Failed to clear prices for product ID {product_id}: {response.json()}") def clear_variation_prices(parent_product_id): variation_ids = get_product_variations(parent_product_id) if not variation_ids: print(f"No variations found for product ID {parent_product_id}") return for variation_id in variation_ids: url = f"{BASE_URL}/products/{parent_product_id}/variations/{variation_id}" data = { "regular_price": "", "sale_price": "" } response = requests.put(url, json=data, auth=(CONSUMER_KEY, CONSUMER_SECRET)) if response.status_code == 200: print(f"Successfully cleared prices for variation ID {variation_id} of product ID {parent_product_id}") else: print(f"Failed to clear prices for variation ID {variation_id} of product ID {parent_product_id}: {response.json()}") def clear_all_product_prices(): product_ids = get_all_products() if not product_ids: print("No products found to clear prices.") return for product_id in product_ids: clear_product_prices(product_id) clear_variation_prices(product_id) if __name__ == "__main__": clear_all_product_prices()
پایان.!
اگر نیاز به آموزشهای بیشتری در ارتباط با وردپرس دارید در بخش نظرات همین نوشته برای ما بنویسید.
برای امتیاز به این نوشته کلیک کنید!
[کل: 0 میانگین: 0]
ارسال پاسخ