2021-02-23 08:36:38 +00:00
|
|
|
import frappe
|
2022-01-31 19:09:14 +00:00
|
|
|
|
2021-02-23 08:36:38 +00:00
|
|
|
from erpnext.e_commerce.doctype.website_item.website_item import make_website_item
|
|
|
|
|
2022-01-31 19:09:14 +00:00
|
|
|
|
2021-02-23 08:36:38 +00:00
|
|
|
def execute():
|
|
|
|
frappe.reload_doc("e_commerce", "doctype", "website_item")
|
2021-09-09 04:17:18 +00:00
|
|
|
frappe.reload_doc("e_commerce", "doctype", "website_item_tabbed_section")
|
|
|
|
frappe.reload_doc("e_commerce", "doctype", "website_offer")
|
|
|
|
frappe.reload_doc("e_commerce", "doctype", "recommended_items")
|
|
|
|
frappe.reload_doc("e_commerce", "doctype", "e_commerce_settings")
|
2021-02-23 08:36:38 +00:00
|
|
|
frappe.reload_doc("stock", "doctype", "item")
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
item_fields = [
|
|
|
|
"item_code",
|
|
|
|
"item_name",
|
|
|
|
"item_group",
|
|
|
|
"stock_uom",
|
|
|
|
"brand",
|
|
|
|
"has_variants",
|
|
|
|
"variant_of",
|
|
|
|
"description",
|
|
|
|
"weightage",
|
|
|
|
]
|
|
|
|
web_fields_to_map = [
|
|
|
|
"route",
|
|
|
|
"slideshow",
|
|
|
|
"website_image_alt",
|
|
|
|
"website_warehouse",
|
|
|
|
"web_long_description",
|
|
|
|
"website_content",
|
2022-06-27 07:30:00 +00:00
|
|
|
"website_image",
|
2022-03-28 13:22:46 +00:00
|
|
|
"thumbnail",
|
|
|
|
]
|
2021-02-23 08:36:38 +00:00
|
|
|
|
2021-10-19 13:20:44 +00:00
|
|
|
# get all valid columns (fields) from Item master DB schema
|
2022-03-28 13:22:46 +00:00
|
|
|
item_table_fields = frappe.db.sql("desc `tabItem`", as_dict=1) # nosemgrep
|
|
|
|
item_table_fields = [d.get("Field") for d in item_table_fields]
|
2021-05-18 09:43:33 +00:00
|
|
|
|
2021-09-02 09:00:37 +00:00
|
|
|
# prepare fields to query from Item, check if the web field exists in Item master
|
2021-05-18 09:43:33 +00:00
|
|
|
web_query_fields = []
|
2021-09-02 09:00:37 +00:00
|
|
|
for web_field in web_fields_to_map:
|
|
|
|
if web_field in item_table_fields:
|
|
|
|
web_query_fields.append(web_field)
|
|
|
|
item_fields.append(web_field)
|
2021-05-18 09:43:33 +00:00
|
|
|
|
|
|
|
# check if the filter fields exist in Item master
|
|
|
|
or_filters = {}
|
|
|
|
for field in ["show_in_website", "show_variant_in_website"]:
|
|
|
|
if field in item_table_fields:
|
|
|
|
or_filters[field] = 1
|
|
|
|
|
|
|
|
if not web_query_fields or not or_filters:
|
|
|
|
# web fields to map are not present in Item master schema
|
|
|
|
# most likely a fresh installation that doesnt need this patch
|
|
|
|
return
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
items = frappe.db.get_all("Item", fields=item_fields, or_filters=or_filters)
|
2021-10-20 08:38:14 +00:00
|
|
|
total_count = len(items)
|
2021-02-23 08:36:38 +00:00
|
|
|
|
2021-10-20 08:38:14 +00:00
|
|
|
for count, item in enumerate(items, start=1):
|
|
|
|
if frappe.db.exists("Website Item", {"item_code": item.item_code}):
|
|
|
|
continue
|
2021-10-19 13:20:44 +00:00
|
|
|
|
2021-10-20 08:38:14 +00:00
|
|
|
# make new website item from item (publish item)
|
|
|
|
website_item = make_website_item(item, save=False)
|
|
|
|
website_item.ranking = item.get("weightage")
|
2021-10-19 12:01:55 +00:00
|
|
|
|
2021-10-20 08:38:14 +00:00
|
|
|
for field in web_fields_to_map:
|
|
|
|
website_item.update({field: item.get(field)})
|
2021-10-19 12:01:55 +00:00
|
|
|
|
2021-10-20 08:38:14 +00:00
|
|
|
website_item.save()
|
2021-10-19 12:01:55 +00:00
|
|
|
|
2021-10-20 08:38:14 +00:00
|
|
|
# move Website Item Group & Website Specification table to Website Item
|
|
|
|
for doctype in ("Website Item Group", "Item Website Specification"):
|
|
|
|
frappe.db.set_value(
|
|
|
|
doctype,
|
2022-03-28 13:22:46 +00:00
|
|
|
{"parenttype": "Item", "parent": item.item_code}, # filters
|
|
|
|
{"parenttype": "Website Item", "parent": website_item.name}, # value dict
|
2021-10-20 08:38:14 +00:00
|
|
|
)
|
2021-02-23 08:36:38 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if count % 20 == 0: # commit after every 20 items
|
2021-09-02 09:00:37 +00:00
|
|
|
frappe.db.commit()
|
2021-10-20 08:38:14 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.utils.update_progress_bar("Creating Website Items", count, total_count)
|