[FIX] product: Add products without 'combination_indices' to cart.

Sometimes when we try to buy a product that doesn't have any variants,
it would result in an error saying there isn't any valid combination
for that product.

This was happening because `_get_variant_id_for_combination` was
considering that the field `combination_indices` would always be
equal to `''` (empty string) in such a case.

This isn't true as `combination_indices` isn't a required field
thus allowing `null` \ `None` to be valid values for that field.

This fix will simply include products with no values set for their
`combination_indices` when searching a corresponding variant from
the `product_template`.

closes odoo/odoo#44056

X-original-commit: 03e7efc3b4592c5f063c665130011a91fcc4d683
Signed-off-by: Toufik Benjaa (tbe) <tbe@odoo.com>
This commit is contained in:
Arnaud Baes
2020-01-27 19:51:58 +00:00
parent c51b027019
commit 97af72330a
+10 -4
View File
@@ -6,6 +6,7 @@ import logging
from odoo import api, fields, models, tools, _, SUPERUSER_ID
from odoo.exceptions import ValidationError, RedirectWarning, UserError
from odoo.osv import expression
_logger = logging.getLogger(__name__)
@@ -894,10 +895,15 @@ class ProductTemplate(models.Model):
Use sudo because the same result should be cached for all users.
"""
self.ensure_one()
return self.env['product.product'].sudo().with_context(active_test=False).search([
('product_tmpl_id', '=', self.id),
('combination_indices', '=', filtered_combination._ids2str())
], order='active DESC', limit=1).id
domain = [('product_tmpl_id', '=', self.id)]
combination_indices_ids = filtered_combination._ids2str()
if combination_indices_ids:
domain = expression.AND([domain, [('combination_indices', '=', combination_indices_ids)]])
else:
domain = expression.AND([domain, [('combination_indices', 'in', ['', False])]])
return self.env['product.product'].sudo().with_context(active_test=False).search(domain, order='active DESC', limit=1).id
@tools.ormcache('self.id')
def _get_first_possible_variant_id(self):