fix: Convert Item links to Website Item links in Item Card Group template data

- Changed link option to Website Item in Item card group template
- patch to convert pre-existing data
This commit is contained in:
marination 2021-12-21 15:43:48 +05:30
parent 262fcbd9fb
commit 456f27724c
4 changed files with 86 additions and 27 deletions

View File

@ -23,9 +23,9 @@
{%- for index in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'] -%}
{%- set item = values['card_' + index + '_item'] -%}
{%- if item -%}
{%- set item = frappe.get_doc("Item", item) -%}
{%- set web_item = frappe.get_doc("Website Item", item) -%}
{{ item_card(
item, is_featured=values['card_' + index + '_featured'],
web_item, is_featured=values['card_' + index + '_featured'],
is_full_width=True, align="Center"
) }}
{%- endif -%}

View File

@ -37,8 +37,8 @@
{
"fieldname": "card_1_item",
"fieldtype": "Link",
"label": "Item",
"options": "Item",
"label": "Website Item",
"options": "Website Item",
"reqd": 0
},
{
@ -56,8 +56,8 @@
{
"fieldname": "card_2_item",
"fieldtype": "Link",
"label": "Item",
"options": "Item",
"label": "Website Item",
"options": "Website Item",
"reqd": 0
},
{
@ -76,8 +76,8 @@
{
"fieldname": "card_3_item",
"fieldtype": "Link",
"label": "Item",
"options": "Item",
"label": "Website Item",
"options": "Website Item",
"reqd": 0
},
{
@ -95,8 +95,8 @@
{
"fieldname": "card_4_item",
"fieldtype": "Link",
"label": "Item",
"options": "Item",
"label": "Website Item",
"options": "Website Item",
"reqd": 0
},
{
@ -114,8 +114,8 @@
{
"fieldname": "card_5_item",
"fieldtype": "Link",
"label": "Item",
"options": "Item",
"label": "Website Item",
"options": "Website Item",
"reqd": 0
},
{
@ -133,8 +133,8 @@
{
"fieldname": "card_6_item",
"fieldtype": "Link",
"label": "Item",
"options": "Item",
"label": "Website Item",
"options": "Website Item",
"reqd": 0
},
{
@ -152,8 +152,8 @@
{
"fieldname": "card_7_item",
"fieldtype": "Link",
"label": "Item",
"options": "Item",
"label": "Website Item",
"options": "Website Item",
"reqd": 0
},
{
@ -171,8 +171,8 @@
{
"fieldname": "card_8_item",
"fieldtype": "Link",
"label": "Item",
"options": "Item",
"label": "Website Item",
"options": "Website Item",
"reqd": 0
},
{
@ -190,8 +190,8 @@
{
"fieldname": "card_9_item",
"fieldtype": "Link",
"label": "Item",
"options": "Item",
"label": "Website Item",
"options": "Website Item",
"reqd": 0
},
{
@ -209,8 +209,8 @@
{
"fieldname": "card_10_item",
"fieldtype": "Link",
"label": "Item",
"options": "Item",
"label": "Website Item",
"options": "Website Item",
"reqd": 0
},
{
@ -228,8 +228,8 @@
{
"fieldname": "card_11_item",
"fieldtype": "Link",
"label": "Item",
"options": "Item",
"label": "Website Item",
"options": "Website Item",
"reqd": 0
},
{
@ -247,8 +247,8 @@
{
"fieldname": "card_12_item",
"fieldtype": "Link",
"label": "Item",
"options": "Item",
"label": "Website Item",
"options": "Website Item",
"reqd": 0
},
{
@ -259,7 +259,7 @@
}
],
"idx": 0,
"modified": "2021-02-24 16:05:31.242342",
"modified": "2021-12-21 14:44:59.821335",
"modified_by": "Administrator",
"module": "E-commerce",
"name": "Item Card Group",

View File

@ -346,3 +346,4 @@ erpnext.patches.v14_0.restore_einvoice_fields
erpnext.patches.v13_0.update_sane_transfer_against
erpnext.patches.v12_0.add_company_link_to_einvoice_settings
erpnext.patches.v14_0.migrate_cost_center_allocations
erpnext.patches.v13_0.convert_to_website_item_in_item_card_group_template

View File

@ -0,0 +1,58 @@
import json
from typing import List
import frappe
from erpnext.e_commerce.doctype.website_item.website_item import make_website_item
def execute():
"""
Convert all Item links to Website Item link values in
exisitng 'Item Card Group' Web Page Block data.
"""
frappe.reload_doc("e_commerce", "web_template", "item_card_group")
blocks = frappe.db.get_all(
"Web Page Block",
filters={"web_template": "Item Card Group"},
fields=["parent", "web_template_values", "name"]
)
fields = generate_fields_to_edit()
for block in blocks:
web_template_value = json.loads(block.get('web_template_values'))
for field in fields:
item = web_template_value.get(field)
if not item:
continue
if frappe.db.exists("Website Item", {"item_code": item}):
website_item = frappe.db.get_value("Website Item", {"item_code": item})
else:
website_item = make_new_website_item(item, web_template_value, field)
continue
if website_item:
web_template_value[field] = website_item
frappe.db.set_value("Web Page Block", block.name, "web_template_values", json.dumps(web_template_value))
def generate_fields_to_edit() -> List:
fields = []
for i in range(1, 13):
fields.append(f"card_{i}_item") # fields like 'card_1_item', etc.
return fields
def make_new_website_item(item, web_template_value, field):
try:
doc = frappe.get_doc("Item", item)
web_item = make_website_item(doc) # returns [website_item.name, item_name]
return web_item[0]
except Exception:
title = f"{item}: Error while converting to Website Item "
frappe.log_error(title + "for Item Card Group Template" + "\n\n" + frappe.get_traceback(), title=title)
return None