diff --git a/product_category_default_ecommerce_cat/__init__.py b/product_category_default_ecommerce_cat/__init__.py new file mode 100644 index 0000000..0650744 --- /dev/null +++ b/product_category_default_ecommerce_cat/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/product_category_default_ecommerce_cat/__manifest__.py b/product_category_default_ecommerce_cat/__manifest__.py new file mode 100644 index 0000000..f22505c --- /dev/null +++ b/product_category_default_ecommerce_cat/__manifest__.py @@ -0,0 +1,21 @@ +# Copyright 2022 Criptomart +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + 'name': 'Product Category Default e-commerce Category', + 'summary': "Set up default public categories based in internal category.", + 'version': '12.0.1.0.0', + 'author': "Criptomart", + 'website': 'https://criptomart.net', + 'license': 'AGPL-3', + 'category': 'Product', + 'depends': [ + 'product', + 'website_sale' + ], + 'data': [ + 'views/product_category.xml', + 'views/res_config_settings_views.xml', + ], + 'installable': True, +} diff --git a/product_category_default_ecommerce_cat/models/__init__.py b/product_category_default_ecommerce_cat/models/__init__.py new file mode 100644 index 0000000..ca02c74 --- /dev/null +++ b/product_category_default_ecommerce_cat/models/__init__.py @@ -0,0 +1,4 @@ +from . import res_company +from . import res_config_settings +from . import product_product +from . import product_category diff --git a/product_category_default_ecommerce_cat/models/product_category.py b/product_category_default_ecommerce_cat/models/product_category.py new file mode 100644 index 0000000..3c45e8c --- /dev/null +++ b/product_category_default_ecommerce_cat/models/product_category.py @@ -0,0 +1,12 @@ + +from odoo import api, fields, models + + +class ProductCategory(models.Model): + _inherit = 'product.category' + + public_categ_id = fields.Many2one( + comodel_name="product.public.category", string="Categoría en la tienda online", + help="Configura los productos nuevos en ésta categoría interna, a la categoría de la tienda online definido aquí.", + ) + diff --git a/product_category_default_ecommerce_cat/models/product_product.py b/product_category_default_ecommerce_cat/models/product_product.py new file mode 100644 index 0000000..3f1f985 --- /dev/null +++ b/product_category_default_ecommerce_cat/models/product_product.py @@ -0,0 +1,46 @@ + +#import logging + +from odoo import api, fields, models + +#_logger = logging.getLogger(__name__) + +class ProductProduct(models.Model): + _inherit = 'product.product' + + @api.model + def create(self, vals): + categ_id = vals.get("categ_id") + template_id = vals.get("product_tmpl_id") + if categ_id: + #check if there are public categories setup by user + new_cats = [] + old_cats = vals.get("public_categ_ids") + if old_cats[0][2]: + for i in old_cats[0][2]: + new_cats.append(i) + category = self.env['product.category'].browse(categ_id) + pub_cat = self.get_category_public_category(category).id + if pub_cat: + new_cats.append(pub_cat) + vals['public_categ_ids'] = [[6,0,new_cats]] + #vals['is_published'] = True + elif template_id: + template = self.env["product.template"].browse(template_id) + category = template.categ_id + pub_cat = self.get_category_public_category(category) + if pub_cat: + template.public_categ_ids += pub_cat + # autopublish + #if template.public_categ_id: + # template.is_published = True + + return super().create(vals) + + @api.model + def get_category_public_category(self, category=False): + cat = category + if not cat.public_categ_id and self.env.user.company_id.use_parent_categories_to_determine_public_cat: + while not cat.public_categ_id and cat.parent_id: + cat = cat.parent_id + return cat.public_categ_id diff --git a/product_category_default_ecommerce_cat/models/res_company.py b/product_category_default_ecommerce_cat/models/res_company.py new file mode 100644 index 0000000..b3be535 --- /dev/null +++ b/product_category_default_ecommerce_cat/models/res_company.py @@ -0,0 +1,13 @@ +# Copyright 2022 Criptomart +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class Company(models.Model): + _inherit = 'res.company' + + use_parent_categories_to_determine_public_cat = fields.Boolean( + string="Use parent categories to determine the ecommerce category", + help="Use parent categories if the category has no set up the public category.", + ) diff --git a/product_category_default_ecommerce_cat/models/res_config_settings.py b/product_category_default_ecommerce_cat/models/res_config_settings.py new file mode 100644 index 0000000..41c347d --- /dev/null +++ b/product_category_default_ecommerce_cat/models/res_config_settings.py @@ -0,0 +1,13 @@ +# Copyright 2022 Criptomart +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = 'res.config.settings' + + use_parent_categories_to_determine_public_cat = fields.Boolean( + related="company_id.use_parent_categories_to_determine_public_cat", + readonly=False, + ) diff --git a/product_category_default_ecommerce_cat/views/product_category.xml b/product_category_default_ecommerce_cat/views/product_category.xml new file mode 100644 index 0000000..04f90d2 --- /dev/null +++ b/product_category_default_ecommerce_cat/views/product_category.xml @@ -0,0 +1,16 @@ + + + + + + product.category.form - product_category_public_categ + product.category + + + + + + + + + diff --git a/product_category_default_ecommerce_cat/views/res_config_settings_views.xml b/product_category_default_ecommerce_cat/views/res_config_settings_views.xml new file mode 100644 index 0000000..a322745 --- /dev/null +++ b/product_category_default_ecommerce_cat/views/res_config_settings_views.xml @@ -0,0 +1,26 @@ + + + + res.config.settings.view.form.inherit.product.public.cat + res.config.settings + + +
+
+
+ +
+
+
+
+
+
+
+ +