This question has been flagged
1 Reply
10993 Views

Hello,


I´m importing with my own python code products and have the problem to update products, which are archived with barcodes.

snippet code:

for row in data['product']:
...
    # check if product exists
product_tmpl_id = self.env['product.product'].search([('barcode', '=ilike', row['barcode'])], limit=1).product_tmpl_id
if not product_tmpl_id:
product_tmpl_id = self.env['product.product'].search([('barcode', '=ilike', row['barcode']), ('active', '=', False)], limit=1).product_tmpl_id
if not product_tmpl_id:
prod = self.env['product.template'].search([('default_code', '=ilike', row['internalReference'])], limit=1)
if not prod:
prod = self.env['product.template'].search([('default_code', '=ilike', row['internalReference']), ('active', '=', False)], limit=1)
else:
logger.info('###### found in second try id %s #######' % product_tmpl_id)
prod = self.env['product.template'].browse(product_tmpl_id)
else:
logger.info('###### found in first try id %s #######' % product_tmpl_id)
prod = self.env['product.template'].browse(product_tmpl_id)

I want to search first, if a product in products.products with Barcode exists.

If yes, he should load the product from product.template

If not, he should check if archived and load then the correct product

if not, he should check for default_code


But this is not working.


How 

Avatar
Discard
Best Answer

Hi,

You can try like this, suppose if you have the barcode in the variable named barcode_sample(you can change accordingly)


Try this  code:

barcode_sample = "contains value to test"
product = self.env['product.product'].search([('barcode', '=', barcode_sample)], limit=1)
if product:

    product_template = product.product_tmpl_id.id
else:

    archieved_product = self.env['product.product'].search([('active', '=', False), ('barcode', '=', barcode_sample)], limit=1)


The above code will check the product.product model for matching barcode in it, if it exists it will return the corresponding product template in the variable product_template. if it does not exist, it will check the archived records in product.product model and return value in archieved_product variable


Thanks

Avatar
Discard
Author

Hello,

thank you! This seems working, but:

I get later errors in my code, because an archieved product miss values, which a active have:

'int' object has no attribute 'seller_ids'

prod.seller_ids.unlink()

I guess if I fix this error, I have later again some errors.

When I found a product, which is archieved ... can I activade it before I continue with my code? So same, as I click in webfront to "active".

Maybe in this way?:

vals = {'active': True} # Active the product

prod.write(vals)

thank you!

In the above code in the variable product_template you will get the id of the record, if you need the record there change the line to

product_template = product.product_tmpl_id

Author

Oh, thank you :-)

Running now fine!