Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odoo 9.0 create Product Variants with python xml-rpc

I am trying to import some Product.templates and their product.variants over Odoo´s xml-rpc api with python.

Actually my code is creating Product.templates with their attribute.line.ids perfectly fine, but somehow the product.variants are not creating, so only one product.variant gets created without any attributes. And I can´t figure out how to do it properly.

So first I am creating a product.template like following (to make it short only insert name here):

id = models.execute_kw(db, uid, password, 'product.template', 'create', [{
'name': "New Partner", 
}])

Afterwards I am adding the attribute.line.ids like this:

for key in attValIdList.keys():
attribute_line = models.execute_kw(db, uid, password, 'product.attribute.line', 'create', [{
    'product_tmpl_id': id,
    'attribute_id': key,
    'value_ids': [(6, 0, attValIdList[key])]
}])

So attValidKeys is a list with dictionaries where I store the attribute_id with their attribute_value_ids.

So this part gets filled correctly. But no product.variants are created out of the product.line.ids..

Actually adding product.product with the attributes is working fine too, but then I am having the issue, that this random product.product without any attributes gets created automatically..

Would be great if you guys could help me out, waisting a lot of hours on this problem.

like image 813
MyPy Avatar asked Nov 09 '22 04:11

MyPy


1 Answers

Odoo.v12 create Product Variants with python xml-rpc

Replace=> product_id=>your product id,variant_name=> your variant name,variant_value=>your veriant value

 product_tmpl_id=models.execute_kw(db, uid, password, 'product.product', 'read', [[[<product_id>]])[0].get('product_tmpl_id','')  
 temp_pro_dta=models.execute_kw(db, uid, password, 'product.product', 'read', [[[<product_id>]])[0].get('attribute_line_ids')
 value_ids=models.execute_kw(db, uid, password, 'product.template.attribute.line', 'read', [temp_pro_dta])
 attrib_ids=models.execute_kw(db, uid, password, 'product.attribute', 'search', [[['name', '=','<variant_name>' ]]])
 if attrib_ids:
        attrib_id=models.execute_kw(db, uid, password, 'product.attribute', 'read', [attrib_ids])[0].get('id')
 else:
        attrib_id=models.execute_kw(db, uid, password, 'product.attribute', 'create', [{"name":"<variant_name>",'create_variant':'always','type':'select'}])
 attrib_value_ids=models.execute_kw(db, uid, password, 'product.attribute.value', 'search', [[['name', '=','<variant_value>'],['attribute_id','=',attrib_id]]])
 if attrib_value_ids:
        attrib_value=models.execute_kw(db, uid, password, 'product.attribute.value', 'read', [attrib_value_ids])[0].get('id')
 else:
        attrib_value=models.execute_kw(db, uid, password, 'product.attribute.value', 'create', [{"name":'<variant_value>','attribute_id':attrib_id}])
 if value_ids:
        value_data_ids=value_ids[0].get('value_ids')
        if attrib_value not in value_data_ids:
                value_data_ids.append(attrib_value)
        attrib_key=models.execute_kw(db, uid, password, 'product.template.attribute.line', 'write', [value_ids[0].get('id'),{'value_ids':[[6,0,value_data_ids]]}])
 else:
        value_ids.append(attrib_value)
        attrib_key=models.execute_kw(db, uid, password, 'product.template.attribute.line', 'create', [{"display_name":"<variant_name>",'product_tmpl_id':product_tmpl_id[0],'attribute_id':attrib_id,'value_ids':[[6,0,value_ids]]}])
like image 78
Akhil Kannan Avatar answered Nov 30 '22 09:11

Akhil Kannan