I want to edit item variant separator from “-” to “.”
Above is my hooks.py override method
plz help
Hi @Atheetha,
In hooks.py
override_whitelisted_methods = {
"erpnext.controllers.item_variant.create_variant": "dev_test_frappe.controllers.override.create_variant"
}
Override create_variant method code.
import json
import frappe
from frappe import _
from frappe.utils import cstr
from erpnext.controllers.item_variant import copy_attributes_to_variant
@frappe.whitelist()
def create_variant(item, args):
if isinstance(args, str):
args = json.loads(args)
template = frappe.get_doc("Item", item)
variant = frappe.new_doc("Item")
variant.variant_based_on = "Item Attribute"
variant_attributes = []
for d in template.attributes:
variant_attributes.append({"attribute": d.attribute, "attribute_value": args.get(d.attribute)})
variant.set("attributes", variant_attributes)
copy_attributes_to_variant(template, variant)
make_variant_item_code(template.item_code, template.item_name, variant)
return variant
def make_variant_item_code(template_item_code, template_item_name, variant):
"""Uses template's item code and abbreviations to make variant's item code"""
if variant.item_code:
return
abbreviations = []
for attr in variant.attributes:
item_attribute = frappe.db.sql(
"""select i.numeric_values, v.abbr
from `tabItem Attribute` i left join `tabItem Attribute Value` v
on (i.name=v.parent)
where i.name=%(attribute)s and (v.attribute_value=%(attribute_value)s or i.numeric_values = 1)""",
{"attribute": attr.attribute, "attribute_value": attr.attribute_value},
as_dict=True,
)
if not item_attribute:
continue
# frappe.throw(_('Invalid attribute {0} {1}').format(frappe.bold(attr.attribute),
# frappe.bold(attr.attribute_value)), title=_('Invalid Attribute'),
# exc=InvalidItemAttributeValueError)
abbr_or_value = (
cstr(attr.attribute_value) if item_attribute[0].numeric_values else item_attribute[0].abbr
)
abbreviations.append(abbr_or_value)
if abbreviations:
variant.item_code = "{}.{}".format(template_item_code, "-".join(abbreviations))
variant.item_name = "{}.{}".format(template_item_name, "-".join(abbreviations))
Output:
Hi…its not working.
override_whitelisted_methods = {
"erpnext.controllers.item_variant.create_variant": "athiapptest.override.create_variant"
}
Its not calling above override whitelisted methods
Check the file path and method.
Then migrate and build the app.
Still not working. This override code has no effect or not overriding.
restart the bench and check it.
I tested it in the custom app, so it works properly.
check the path
Only refreshing or reloading page this error is showing. Let me know
Please reply
Hi
I Just want to know is it a good practice to edit in init.py. Kindly waiting for your reply. Thank you in advance.