website_sale_comparison.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. from collections import OrderedDict
  3. from odoo import fields, models
  4. class ProductAttributeCategory(models.Model):
  5. _name = "product.attribute.category"
  6. _description = "Product Attribute Category"
  7. _order = 'sequence, id'
  8. name = fields.Char("Category Name", required=True, translate=True)
  9. sequence = fields.Integer("Sequence", default=10, index=True)
  10. attribute_ids = fields.One2many('product.attribute', 'category_id', string="Related Attributes", domain="[('category_id', '=', False)]")
  11. class ProductAttribute(models.Model):
  12. _inherit = 'product.attribute'
  13. _order = 'category_id, sequence, id'
  14. category_id = fields.Many2one('product.attribute.category', string="Category", index=True,
  15. help="Set a category to regroup similar attributes under "
  16. "the same section in the Comparison page of eCommerce")
  17. class ProductTemplateAttributeLine(models.Model):
  18. _inherit = 'product.template.attribute.line'
  19. def _prepare_categories_for_display(self):
  20. """On the product page group together the attribute lines that concern
  21. attributes that are in the same category.
  22. The returned categories are ordered following their default order.
  23. :return: OrderedDict [{
  24. product.attribute.category: [product.template.attribute.line]
  25. }]
  26. """
  27. attributes = self.attribute_id
  28. categories = OrderedDict([(cat, self.env['product.template.attribute.line']) for cat in attributes.category_id.sorted()])
  29. if any(not pa.category_id for pa in attributes):
  30. # category_id is not required and the mapped does not return empty
  31. categories[self.env['product.attribute.category']] = self.env['product.template.attribute.line']
  32. for ptal in self:
  33. categories[ptal.attribute_id.category_id] |= ptal
  34. return categories
  35. class ProductProduct(models.Model):
  36. _inherit = 'product.product'
  37. def _prepare_categories_for_display(self):
  38. """On the comparison page group on the same line the values of each
  39. product that concern the same attributes, and then group those
  40. attributes per category.
  41. The returned categories are ordered following their default order.
  42. :return: OrderedDict [{
  43. product.attribute.category: OrderedDict [{
  44. product.attribute: OrderedDict [{
  45. product: [product.template.attribute.value]
  46. }]
  47. }]
  48. }]
  49. """
  50. attributes = self.product_tmpl_id.valid_product_template_attribute_line_ids._without_no_variant_attributes().attribute_id.sorted()
  51. categories = OrderedDict([(cat, OrderedDict()) for cat in attributes.category_id.sorted()])
  52. if any(not pa.category_id for pa in attributes):
  53. # category_id is not required and the mapped does not return empty
  54. categories[self.env['product.attribute.category']] = OrderedDict()
  55. for pa in attributes:
  56. categories[pa.category_id][pa] = OrderedDict([(
  57. product,
  58. product.product_template_attribute_value_ids.filtered(lambda ptav: ptav.attribute_id == pa)
  59. ) for product in self])
  60. return categories