Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] product_configurator_custom_value_variant #147

Open
wants to merge 2 commits into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions product_configurator_custom_value_variant/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
====================================================================
Product Configurator Sale - Create product variant from custom value
====================================================================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:7573027e875a83e706040e15f005280aedfc622c5f140774594e002abaf12a2f
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fproduct--configurator-lightgray.png?logo=github
:target: https://github.com/OCA/product-configurator/tree/16.0/product_configurator_custom_value_variant
:alt: OCA/product-configurator
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/product-configurator-16-0/product-configurator-16-0-product_configurator_custom_value_variant
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/product-configurator&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

Create variants when custom attributes are used during product configuration.

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/product-configurator/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/product-configurator/issues/new?body=module:%20product_configurator_custom_value_variant%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Aion Tech

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/product-configurator <https://github.com/OCA/product-configurator/tree/16.0/product_configurator_custom_value_variant>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
3 changes: 3 additions & 0 deletions product_configurator_custom_value_variant/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import models
19 changes: 19 additions & 0 deletions product_configurator_custom_value_variant/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2025 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Product Configurator Sale - Create product variant from custom value",
"version": "16.0.1.0.0",
"category": "Sales/Sales",
"summary": "Allow to create variant for custom values for configurable products.",
"author": "Aion Tech, Odoo Community Association (OCA)",
"license": "AGPL-3",
"website": "https://github.com/OCA/product-configurator",
"depends": [
"product_configurator_sale",
"product_attribute_custom_value_variant",
],
"auto_install": True,
"data": [
"views/product_attribute_views.xml",
],
}
4 changes: 4 additions & 0 deletions product_configurator_custom_value_variant/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import product_attribute
from . import product_config_session
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2025 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class ProductAttribute(models.Model):
_inherit = "product.attribute"

create_attribute_value = fields.Boolean(
string="Create attribute value",
help="When this custom attribute is used, \
a new value will be generated.",
)

@api.constrains(
"create_attribute_value",
"val_custom",
)
def _constrain_create_attribute_value(self):
for value in self:
if value.create_attribute_value and not value.val_custom:
raise ValidationError(

Check warning on line 24 in product_configurator_custom_value_variant/models/product_attribute.py

View check run for this annotation

Codecov / codecov/patch

product_configurator_custom_value_variant/models/product_attribute.py#L24

Added line #L24 was not covered by tests
_("'Create attribute value' can only be set on custom values")
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright 2025 Simone Rubino - Aion Tech
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models


class ProductConfigSession(models.Model):
_inherit = "product.config.session"

def _create_custom_attribute_values(self, value_ids=None, custom_vals=None):
"""Create new attribute values and assign them to `self`."""
if value_ids is None:
value_ids = self.value_ids.ids

if custom_vals is None:
custom_vals = self._get_custom_vals_dict()

new_attribute_values = self.env["product.attribute.value"].browse()
session_custom_values_to_unlink = self.env[
"product.config.session.custom.value"
].browse()
for attribute_id, custom_value in custom_vals.copy().items():
attribute = self.env["product.attribute"].browse(attribute_id)
if attribute.create_attribute_value:
custom_attribute_value = attribute._get_variant_custom_attribute_value(
str(custom_value)
)

value_ids.append(custom_attribute_value.id)
new_attribute_values |= custom_attribute_value

custom_vals.pop(attribute_id, None)
session_custom_value_to_unlink = self.custom_value_ids.filtered(
lambda session_custom_value: session_custom_value.attribute_id
== attribute
)
if session_custom_value_to_unlink:
session_custom_values_to_unlink |= session_custom_value_to_unlink

if new_attribute_values:
self.value_ids |= new_attribute_values
self.value_ids -= self.get_custom_value_id()
for attribute in new_attribute_values.attribute_id:
attribute_line = self.product_tmpl_id.attribute_line_ids.filtered(
lambda ptav: ptav.attribute_id == attribute
)
attribute_line.value_ids |= new_attribute_values.filtered(
lambda pav: pav.attribute_id == attribute
)

if session_custom_values_to_unlink:
session_custom_values_to_unlink.unlink()

return new_attribute_values

def create_get_variant(
self,
value_ids=None,
custom_vals=None,
):
new_attribute_values = self._create_custom_attribute_values(
value_ids=value_ids,
custom_vals=custom_vals,
)
variant = super().create_get_variant(
value_ids=value_ids,
custom_vals=custom_vals,
)

if new_attribute_values:
# The new attribute values must not be available for new models
for attribute_line in self.product_tmpl_id.attribute_line_ids:
attribute_line.with_context(
no_remove_custom_variants=variant.ids,
).value_ids -= (
attribute_line.value_ids & new_attribute_values
)
return variant
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Create variants when custom attributes are used during product configuration.
Loading
Loading