Customizing erpnext/controllers/item_variant.py

I want to edit item variant separator from “-” to “.”


Above is my hooks.py override method
plz help

1 Like

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.

Method Not Allowed

You are not permitted to access this resource.Function athiapp.override.enqueue_multiple_variant_creation is not whitelisted.
Received above errror

check the path

In google I found another method by editing init.py file of app

__version__ = "0.0.1"
from erpnext.controllers import item_variant
from testapp.override import make_variant_item_code
item_variant.make_variant_item_code = make_variant_item_code

Its working
But I am getting this error message

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.

Check it.