This commit is contained in:
Jens Falk
2026-06-06 20:46:13 +02:00
parent 18555bf5bb
commit 324e8cadf7
2 changed files with 6 additions and 17 deletions
+5 -16
View File
@@ -4,8 +4,7 @@ from frappe import _
@frappe.whitelist()
def sync_custom_fields_to_woocommerce(item_code):
"""Sync Custom Fields vom Item nach WooCommerce ACF
Liest die Zuordnungen dynamisch aus dem DocType 'WooCommerce ACF Mapping'"""
"""Sync Custom Fields vom Item nach WooCommerce ACF (dynamisch)"""
if not item_code:
frappe.throw(_("Kein Item Code angegeben"))
@@ -19,7 +18,7 @@ def sync_custom_fields_to_woocommerce(item_code):
frappe.msgprint(_("Item hat keine verknüpften WooCommerce Server"), indicator="orange")
return False
# Dynamisches Mapping aus dem DocType laden
# Dynamisches Mapping aus DocType laden
field_mapping = {}
mappings = frappe.get_all("WooCommerce ACF Mapping",
filters={"enabled": 1},
@@ -30,7 +29,7 @@ def sync_custom_fields_to_woocommerce(item_code):
field_mapping[m.erp_field] = m.acf_field
if not field_mapping:
frappe.msgprint(_("Keine aktivierten Mappings im DocType 'WooCommerce ACF Mapping' gefunden"),
frappe.msgprint(_("Keine aktivierten Mappings im DocType 'WooCommerce ACF Mapping' gefunden."),
indicator="orange")
return False
@@ -48,7 +47,6 @@ def sync_custom_fields_to_woocommerce(item_code):
errors.append("WooCommerce Server {} hat keine Zugangsdaten".format(wc_link.woocommerce_server))
continue
# Basis URL vorbereiten
base_url = wc_server.woocommerce_server_url.rstrip("/")
if not base_url.endswith("/wp-json/wc/v3"):
base_url += "/wp-json/wc/v3"
@@ -56,12 +54,10 @@ def sync_custom_fields_to_woocommerce(item_code):
url = "{}/products/{}".format(base_url, wc_link.woocommerce_id)
auth = (wc_server.api_key, wc_server.api_secret)
# Produkt holen
resp = requests.get(url, auth=auth, timeout=15)
resp.raise_for_status()
product = resp.json()
# ACF Daten mergen
acf_data = product.get("acf", {})
updated_fields = 0
@@ -76,27 +72,20 @@ def sync_custom_fields_to_woocommerce(item_code):
payload = {"acf": acf_data}
# Update durchführen
update_resp = requests.put(url, json=payload, auth=auth, timeout=15)
update_resp.raise_for_status()
success_count += 1
except requests.exceptions.RequestException as e:
error_msg = "API-Fehler für Produkt {}: {}".format(wc_link.woocommerce_id, str(e))
errors.append(error_msg)
frappe.log_error(error_msg, "WooCommerce ACF Sync")
except Exception as e:
error_msg = "Unerwarteter Fehler für Item {} / WC-ID {}: {}".format(item_code, wc_link.woocommerce_id, str(e))
error_msg = "Fehler für Item {} / WC-ID {}: {}".format(item_code, wc_link.woocommerce_id, str(e))
errors.append(error_msg)
frappe.log_error(error_msg, "WooCommerce ACF Sync")
# Zusammenfassung
if success_count > 0:
msg = "Custom Fields für {} WooCommerce-Produkt(e) erfolgreich synchronisiert".format(success_count)
if errors:
msg += " ({} Fehler aufgetreten)".format(len(errors))
msg += " ({} Fehler)".format(len(errors))
frappe.msgprint(msg, alert=True, indicator="green")
return True
else:
+1 -1
View File
@@ -1,7 +1,7 @@
[project]
name = "erpnext_custom"
version = "0.0.1"
description = "Custom App für NEXTErp"
description = "Custom App für NEXTErp - WooCommerce ACF Sync"
readme = "README.md"
requires-python = ">=3.10"