From ddea5dd9feb39c2c19beada9c546b319bb2dc077 Mon Sep 17 00:00:00 2001 From: Simone Rubino Date: Wed, 31 Jan 2024 16:08:03 +0100 Subject: [PATCH] [IMP] website_product_configurator: Reconfigure cart line --- website_product_configurator/README.rst | 2 +- website_product_configurator/__manifest__.py | 1 + .../controllers/main.py | 63 ++++++++++++++- .../data/config_form_templates.xml | 35 ++++++++ .../models/sale_order.py | 25 +++++- .../static/description/index.html | 2 +- .../static/src/js/website_sale.esm.js | 6 ++ .../tests/tours/reconfigure_cart_line.esm.js | 80 +++++++++++++++++++ .../tests/__init__.py | 1 + .../tests/test_reconfigure_cart_line.py | 38 +++++++++ .../views/templates.xml | 24 ++++++ 11 files changed, 269 insertions(+), 8 deletions(-) create mode 100644 website_product_configurator/static/tests/tours/reconfigure_cart_line.esm.js create mode 100644 website_product_configurator/tests/test_reconfigure_cart_line.py diff --git a/website_product_configurator/README.rst b/website_product_configurator/README.rst index 09f1bfe434..a88d44e00e 100644 --- a/website_product_configurator/README.rst +++ b/website_product_configurator/README.rst @@ -7,7 +7,7 @@ Website Product Configurator !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:571a6836c9659722dc42db79a4b0b1d7f1915bf9c139a7a34518f21973cdc009 + !! source digest: sha256:5cb6dd72d2d950eb9a268d8cc613db21a735494ec532a55bd472156be9a88b76 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png diff --git a/website_product_configurator/__manifest__.py b/website_product_configurator/__manifest__.py index 29a6890ac4..ebbe5c6e5d 100644 --- a/website_product_configurator/__manifest__.py +++ b/website_product_configurator/__manifest__.py @@ -31,6 +31,7 @@ ], "web.assets_tests": [ "website_product_configurator/static/tests/tours/custom_values.esm.js", + "website_product_configurator/static/tests/tours/reconfigure_cart_line.esm.js", ], }, "demo": ["demo/product_template_demo.xml"], diff --git a/website_product_configurator/controllers/main.py b/website_product_configurator/controllers/main.py index 2d72029465..26bb3604b5 100644 --- a/website_product_configurator/controllers/main.py +++ b/website_product_configurator/controllers/main.py @@ -179,6 +179,14 @@ def render_form( config_vals = self.get_render_vals(cfg_session) values.update(product_values) values.update(config_vals) + reconfiguring_order_line_id = kwargs.get("reconfiguring_order_line_id") + if reconfiguring_order_line_id: + reconfiguring_order_line = request.env["sale.order.line"].browse( + int(reconfiguring_order_line_id) + ) + order = request.website.sale_get_order() + if reconfiguring_order_line.exists() in order.order_line: + values["reconfiguring_order_line"] = reconfiguring_order_line return request.render( "website_product_configurator.product_configurator", values ) @@ -456,11 +464,11 @@ def save_configuration( except Exception as Ex: return {"error": Ex} - form_values = self.get_orm_form_vals(form_values, config_session_id) + config_form_values = self.get_orm_form_vals(form_values, config_session_id) try: # save values config_session_id.sudo().update_session_configuration_value( - vals=form_values, product_tmpl_id=product_template_id + vals=config_form_values, product_tmpl_id=product_template_id ) # next step @@ -495,6 +503,19 @@ def save_configuration( if product: redirect_url = "/product_configurator/product" redirect_url += "/%s" % (slug(config_session_id)) + reconfiguring_order_line_id_list = list( + filter( + lambda v: v["name"] == "reconfiguring_order_line_id", + form_values, + ) + ) + if reconfiguring_order_line_id_list: + reconfiguring_order_line_id = reconfiguring_order_line_id_list[0][ + "value" + ] + redirect_url += "?reconfiguring_order_line_id={}".format( + reconfiguring_order_line_id + ) return { "product_id": product.id, "config_session": config_session_id.id, @@ -552,6 +573,15 @@ def cfg_session(self, cfg_session_id, **post): "vals": vals, "reconfigure_product_url": reconfigure_product_url, } + reconfiguring_order_line_id = post.get("reconfiguring_order_line_id") + if reconfiguring_order_line_id: + reconfiguring_order_line = request.env["sale.order.line"].browse( + int(reconfiguring_order_line_id) + ) + order = request.website.sale_get_order() + if reconfiguring_order_line.exists() in order.order_line: + values["add_qty"] = int(reconfiguring_order_line.product_uom_qty) + values["reconfiguring_order_line"] = reconfiguring_order_line return request.render("website_product_configurator.cfg_product", values) @http.route( @@ -568,7 +598,14 @@ def reconfigure_product(self, product_id, **post): tmpl_value_ids = product_id.product_template_attribute_value_ids cfg_session.value_ids = tmpl_value_ids.mapped("product_attribute_value_id") cfg_session.product_id = product_id.id - return request.redirect("/shop/product/%s" % (slug(product_tmpl_id))) + + redirect_url = "/shop/product/%s" % (slug(product_tmpl_id)) + reconfiguring_order_line_id = post.get("reconfiguring_order_line_id") + if reconfiguring_order_line_id: + redirect_url += "?reconfiguring_order_line_id={}".format( + reconfiguring_order_line_id + ) + return request.redirect(redirect_url) except Exception: error_code = 1 return request.redirect( @@ -595,6 +632,26 @@ def render_error(self, error=None, message="", **post): vals = {"message": message, "error": error} return request.render("website_product_configurator.error_page", vals) + @http.route() + def cart_update_json(self, product_id, **kwargs): + reconfiguring_order_line_id = kwargs.get("reconfiguring_order_line_id") + line_id = kwargs.get("line_id") + if reconfiguring_order_line_id and not line_id: + reconfiguring_order_line = request.env["sale.order.line"].browse( + int(reconfiguring_order_line_id) + ) + order = request.website.sale_get_order() + if reconfiguring_order_line.exists() in order.order_line: + # Set (not add) the `add_qty` in the line we are reconfiguring. + kwargs["line_id"] = reconfiguring_order_line.id + kwargs["set_qty"] = kwargs.pop("add_qty", 1) + # Product has to be changed too + # otherwise the line we are reconfiguring is discarded + # because doesn't match the received product + kwargs["reconfiguring_product_id"] = product_id + product_id = reconfiguring_order_line.product_id.id + return super().cart_update_json(product_id, **kwargs) + @http.route() def cart_update(self, product_id, add_qty=1, set_qty=0, **kw): """This route is called when adding a product to cart (no options).""" diff --git a/website_product_configurator/data/config_form_templates.xml b/website_product_configurator/data/config_form_templates.xml index 607bc289b3..38f87960a5 100644 --- a/website_product_configurator/data/config_form_templates.xml +++ b/website_product_configurator/data/config_form_templates.xml @@ -16,6 +16,16 @@ + + +