How to copy values from template to Variant?

When we create a Variant from a template then all the fields from template are copied to the variant (only if the field is not set to no_copy).

Now I would like to better this logic and I am trying to copy all the values from the template even when a variant is updated. This would ensure that the values in the template and its variant are all same at all times without a bypass.

I am trying to accomplish the same via this code:

def validate_variants(doc,method):
	if doc.variant_of:
		#Check if all variants are mentioned in the Item Variant Table as per the Template.
		template = frappe.get_doc("Item", doc.variant_of)
		
		template_attribute = []
		variant_attribute = []
		template_restricted_attributes = {}
		template_rest_summary = []
			
		copy_template_fields(template, doc)

def copy_template_fields(item, doc):
	from frappe.model import no_value_fields
	for field in item.meta.fields:
		if field.fieldtype not in no_value_fields and (not field.no_copy)\
			and field.fieldname not in ("item_code", "item_name", "show_in_website",\
			"variant_of", "description", "web_long_description"):
			if doc.get(field.fieldname) != item.get(field.fieldname):
				change_field = field.fieldname
				doc.change_field = item.get(field.fieldname)
	doc.variant_of = item.name
	doc.has_variants = 0

Now this code is working fine since it returns correct values but it is unable to copy the values of the fields from the template.

Can any one help me with this problem.

@adityaduggal does removing no copy help you ? Also if you make changes in the template the variants are updated by the system. Maybe I didn’t understand your query.

Well this is true when we make changes to the template then the changes are automatically copied to the Variant but what about if we make changes to the variant for fields which are supposed to be copied.

Let me give an example:

  1. Suppose the default warehouse in the Template is Set to “XYZ” now the moment we save a template this copied to the variant. Till here we are on the same page.
  2. Now if I change the default warehouse of the Variant to “ABC” then this also stands true and the system would not copy the XYZ from the template since we are only making the changes to the variant and not the template.

Now I have an issue with point no 2. The reason is that if a field is SET to be COPIED from template then if someone changes the field in variant then those changes should not be allowed as it would be false change since any changes made would invariably be lost if someone changes the Template.

I hope the above example clears my query and that is what I am trying to do with my custom script.

Yes i understand the issue. its a bug and needs to be fixed in the standard product itself. Can you raise a github issue. i will fix it up.