diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/sale_product_configurator/models | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/sale_product_configurator/models')
| -rw-r--r-- | addons/sale_product_configurator/models/__init__.py | 4 | ||||
| -rw-r--r-- | addons/sale_product_configurator/models/product.py | 43 | ||||
| -rw-r--r-- | addons/sale_product_configurator/models/sale_order_line.py | 11 |
3 files changed, 58 insertions, 0 deletions
diff --git a/addons/sale_product_configurator/models/__init__.py b/addons/sale_product_configurator/models/__init__.py new file mode 100644 index 00000000..3873985e --- /dev/null +++ b/addons/sale_product_configurator/models/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- + +from . import sale_order_line +from . import product diff --git a/addons/sale_product_configurator/models/product.py b/addons/sale_product_configurator/models/product.py new file mode 100644 index 00000000..9fea6201 --- /dev/null +++ b/addons/sale_product_configurator/models/product.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import fields, models, api + + +class ProductTemplate(models.Model): + _inherit = 'product.template' + _check_company_auto = True + + optional_product_ids = fields.Many2many( + 'product.template', 'product_optional_rel', 'src_id', 'dest_id', + string='Optional Products', help="Optional Products are suggested " + "whenever the customer hits *Add to Cart* (cross-sell strategy, " + "e.g. for computers: warranty, software, etc.).", check_company=True) + + @api.depends('attribute_line_ids.value_ids.is_custom', 'attribute_line_ids.attribute_id.create_variant') + def _compute_has_configurable_attributes(self): + """ A product is considered configurable if: + - It has dynamic attributes + - It has any attribute line with at least 2 attribute values configured + - It has at least one custom attribute value """ + for product in self: + product.has_configurable_attributes = any(attribute.create_variant == 'dynamic' for attribute in product.mapped('attribute_line_ids.attribute_id')) \ + or any(len(attribute_line_id.value_ids) >= 2 for attribute_line_id in product.attribute_line_ids) \ + or any(attribute_value.is_custom for attribute_value in product.mapped('attribute_line_ids.value_ids')) + + def get_single_product_variant(self): + """ Method used by the product configurator to check if the product is configurable or not. + + We need to open the product configurator if the product: + - is configurable (see has_configurable_attributes) + - has optional products """ + self.ensure_one() + res = super(ProductTemplate, self).get_single_product_variant() + if res.get('product_id', False): + has_optional_products = False + for optional_product in self.product_variant_id.optional_product_ids: + if optional_product.has_dynamic_attributes() or optional_product._get_possible_variants(self.product_variant_id.product_template_attribute_value_ids): + has_optional_products = True + break + res.update({'has_optional_products': has_optional_products}) + return res diff --git a/addons/sale_product_configurator/models/sale_order_line.py b/addons/sale_product_configurator/models/sale_order_line.py new file mode 100644 index 00000000..62d613a9 --- /dev/null +++ b/addons/sale_product_configurator/models/sale_order_line.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import fields, models + + +class SaleOrderLine(models.Model): + _inherit = "sale.order.line" + + is_configurable_product = fields.Boolean('Is the product configurable?', related="product_template_id.has_configurable_attributes") + product_template_attribute_value_ids = fields.Many2many(related='product_id.product_template_attribute_value_ids', readonly=True) |
