From 9506dbe43385f7e80b9d772a29de304814c9b51b Mon Sep 17 00:00:00 2001 From: marination Date: Mon, 18 Apr 2022 21:38:22 +0530 Subject: [PATCH] chore: Patch to copy custom fields (field filters) from Item to Website Item --- erpnext/patches.txt | 1 + ...py_custom_field_filters_to_website_item.py | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 erpnext/patches/v13_0/copy_custom_field_filters_to_website_item.py diff --git a/erpnext/patches.txt b/erpnext/patches.txt index b2d0871a17c..5d836fc2d72 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -359,3 +359,4 @@ erpnext.patches.v13_0.enable_ksa_vat_docs #1 erpnext.patches.v13_0.create_gst_custom_fields_in_quotation erpnext.patches.v13_0.update_expense_claim_status_for_paid_advances erpnext.patches.v13_0.set_return_against_in_pos_invoice_references +erpnext.patches.v13_0.copy_custom_field_filters_to_website_item diff --git a/erpnext/patches/v13_0/copy_custom_field_filters_to_website_item.py b/erpnext/patches/v13_0/copy_custom_field_filters_to_website_item.py new file mode 100644 index 00000000000..5f2125144fe --- /dev/null +++ b/erpnext/patches/v13_0/copy_custom_field_filters_to_website_item.py @@ -0,0 +1,54 @@ +import frappe +from frappe.custom.doctype.custom_field.custom_field import create_custom_field + + +def execute(): + "Add Field Filters, that are not standard fields in Website Item, as Custom Fields." + settings = frappe.get_doc("E Commerce Settings") + + if not (settings.filter_fields or settings.field_filters): + return + + item_meta = frappe.get_meta("Item") + valid_item_fields = [ + df.fieldname for df in item_meta.fields if df.fieldtype in ["Link", "Table MultiSelect"] + ] + + web_item_meta = frappe.get_meta("Website Item") + valid_web_item_fields = [ + df.fieldname for df in web_item_meta.fields if df.fieldtype in ["Link", "Table MultiSelect"] + ] + + for row in settings.filter_fields: + # skip if illegal field + if row.fieldname not in valid_item_fields: + continue + + # if Item field is not in Website Item, add it as a custom field + if row.fieldname not in valid_web_item_fields: + df = item_meta.get_field(row.fieldname) + create_custom_field( + "Website Item", + dict( + owner="Administrator", + fieldname=df.fieldname, + label=df.label, + fieldtype=df.fieldtype, + options=df.options, + description=df.description, + read_only=df.read_only, + no_copy=df.no_copy, + insert_after="on_backorder", + ), + ) + + # map field values + frappe.db.sql( + """ + UPDATE `tabWebsite Item` wi, `tabItem` i + SET wi.{0} = i.{0} + WHERE wi.item_code = i.item_code + """.format( + row.fieldname + ) + )