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

[16.0][MIG] website sale product assortment #854

Closed
Closed
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
6 changes: 6 additions & 0 deletions setup/website_sale_product_assortment/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
109 changes: 109 additions & 0 deletions website_sale_product_assortment/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
============================
eCommerce product assortment
============================

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

.. |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%2Fe--commerce-lightgray.png?logo=github
:target: https://github.com/OCA/e-commerce/tree/15.0/website_sale_product_assortment
:alt: OCA/e-commerce
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/e-commerce-15-0/e-commerce-15-0-website_sale_product_assortment
: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/e-commerce&target_branch=15.0
:alt: Try me on Runboat

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

This module allows to set e-commerce restrictions on product assortments.

**Table of contents**

.. contents::
:local:

Configuration
=============

To see this module working, you have to define a product assortment and select
an option on the website availability field.

#. **Don't apply restriction**: This option will not set any kind of restriction on
product items.
#. **Avoid to show non available products**: This option will hide on the e-commerce, the
products that are not added to the products domain. If a product template has at least
one allowed variant to show, the product will appear on the product items view but only
that variants will be able to be bought.
#. **Avoid selling not available products**: This option will restrict to buy the
products that are added to the assortment on the e-commerce. To inform the clients,
two more fields were added: "Message when unavailable" and "Assortment information".
The first one will add a short description to the product item and the other one will set a
detailed description on the product sheet. This second one is editable from the website editor.

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/e-commerce/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/e-commerce/issues/new?body=module:%20website_sale_product_assortment%0Aversion:%2015.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
~~~~~~~

* Tecnativa

Contributors
~~~~~~~~~~~~

* `Tecnativa <https://www.tecnativa.com>`_:

* Carlos Roca
* Pedro M. Baeza
* Stefan Ungureanu

* `Ooops <https://www.ooops404.com>`_:
* Ashish Hirpara (https://ashish-hirpara.com)

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.

.. |maintainer-CarlosRoca13| image:: https://github.com/CarlosRoca13.png?size=40px
:target: https://github.com/CarlosRoca13
:alt: CarlosRoca13

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-CarlosRoca13|

This module is part of the `OCA/e-commerce <https://github.com/OCA/e-commerce/tree/15.0/website_sale_product_assortment>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions website_sale_product_assortment/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import controllers
from . import models
30 changes: 30 additions & 0 deletions website_sale_product_assortment/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2021 Tecnativa - Carlos Roca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "eCommerce product assortment",
"summary": "Use product assortments to display products available on e-commerce.",
"version": "16.0.1.0.0",
"development_status": "Beta",
"license": "AGPL-3",
"category": "Website",
"website": "https://github.com/OCA/e-commerce",
"author": "Tecnativa, Odoo Community Association (OCA)",
"maintainers": ["CarlosRoca13"],
"installable": True,
"depends": ["product_assortment", "website_sale"],
"data": [
"views/ir_filters_views.xml",
],
"assets": {
"web.assets_frontend": [
"website_sale_product_assortment/static/src/js/variant_mixin.js",
"website_sale_product_assortment/static/src/js/assortment_list_preview.js",
"website_sale_product_assortment/static/src/xml/product_availability.xml",
],
"web.assets_tests": [
"website_sale_product_assortment/static/src/tests/tours/no_purchase_tour.js",
"website_sale_product_assortment/static/src/tests/tours/no_restriction_tour.js",
"website_sale_product_assortment/static/src/tests/tours/no_show_tour.js",
],
},
}
2 changes: 2 additions & 0 deletions website_sale_product_assortment/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import variant
from . import website_sale
42 changes: 42 additions & 0 deletions website_sale_product_assortment/controllers/variant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2020 Tecnativa - Carlos Roca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import _, http
from odoo.http import request

from odoo.addons.sale.controllers.variant import VariantController


class WebsiteSaleVariantController(VariantController):
@http.route(
["/sale/get_info_assortment_preview"],
type="json",
auth="public",
methods=["POST"],
website=True,
)
def get_info_assortment_preview(self, product_template_ids, **kw):
"""Special route to use website logic in get_combination_info override.
This route is called in JS by appending _website to the base route.
"""
res = []
templates = request.env["product.template"].sudo().browse(product_template_ids)
not_allowed_product_dict = templates.get_product_assortment_restriction_info(
templates.mapped("product_variant_ids.id")
)
for template in templates:
variant_ids = set(template.product_variant_ids.ids)
if (
variant_ids
and variant_ids & set(not_allowed_product_dict.keys()) == variant_ids
):
res.append(
{
"id": template.id,
"message_unavailable": not_allowed_product_dict[
variant_ids.pop()
][0].message_unavailable
or _("Not available"),
}
)
return res
79 changes: 79 additions & 0 deletions website_sale_product_assortment/controllers/website_sale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright 2021 Tecnativa - Carlos Roca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from werkzeug.exceptions import NotFound

from odoo.http import request, route

from odoo.addons.website_sale.controllers.main import WebsiteSale


class WebsiteSale(WebsiteSale):
def _get_products_allowed(self):
partner = request.env.user.partner_id
website_id = request.website.id
assortments = (
request.env["ir.filters"]
.sudo()
.search(
[
("is_assortment", "=", True),
("website_availability", "=", "no_show"),
"|",
("website_ids", "=", False),
("website_ids", "=", website_id),
]
)
)
assortment_restriction = False
allowed_product_ids = set()
for assortment in assortments:
if (
# Set active_test to False to allow filtering by partners
# that are not active, (for example Public User)
partner
& assortment.with_context(active_test=False).all_partner_ids
):
assortment_restriction = True
allowed_product_ids = allowed_product_ids.union(
set(assortment.all_product_ids.ids)
)
return allowed_product_ids, assortment_restriction

@route()
def product(self, product, category="", search="", **kwargs):
"""Overriding product method to avoid accessing to product sheet when the
product assortments prevent to show them.
"""
allowed_product_ids, assortment_restriction = self._get_products_allowed()
if assortment_restriction:
if len(set(product.product_variant_ids.ids) & allowed_product_ids) == 0:
raise NotFound()
return super().product(product, category=category, search=search, **kwargs)

def _get_search_options(
self,
category=None,
attrib_values=None,
pricelist=None,
min_price=0.0,
max_price=0.0,
conversion_rate=1,
**post
):
"""Overriding _get_search_options method to avoid show product templates that
has all their variants not allowed to be shown."""
res = super()._get_search_options(
category=category,
attrib_values=attrib_values,
pricelist=pricelist,
min_price=min_price,
max_price=max_price,
conversion_rate=conversion_rate,
**post
)
allowed_product_ids, assortment_restriction = self._get_products_allowed()
if assortment_restriction:
res["allowed_product_domain"] = [
("product_variant_ids", "in", list(allowed_product_ids))
]
return res
Loading