Skip to content

Commit

Permalink
[FIX] website_sale_restrict_sepa_dd
Browse files Browse the repository at this point in the history
  • Loading branch information
remytms committed Dec 3, 2024
1 parent 4e50167 commit aef94c7
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 22 deletions.
1 change: 1 addition & 0 deletions website_sale_restrict_sepa_dd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
#
# SPDX-License-Identifier: AGPL-3.0-or-later
from . import models
from . import controllers
4 changes: 4 additions & 0 deletions website_sale_restrict_sepa_dd/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later
from . import main
57 changes: 57 additions & 0 deletions website_sale_restrict_sepa_dd/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# SPDX-FileCopyrightText: 2024 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from odoo import http
from odoo.http import request

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


class WebsiteSaleRestrictSEPADD(WebsiteSale):
@http.route(
["/shop/cart/update"],
type="http",
auth="public",
methods=["POST"],
website=True,
)
def cart_update(
self,
product_id,
add_qty=1,
set_qty=0,
product_custom_attribute_values=None,
no_variant_attribute_values=None,
express=False,
**kwargs
):
sale_order = request.website.sale_get_order(force_create=True)
if sale_order.state != "draft":
request.session["sale_order_id"] = None
sale_order = request.website.sale_get_order(force_create=True)

warning = sale_order.check_product_compatibility(int(product_id))

request.session["website_sale_cart_warning"] = warning

if warning:
return request.redirect("/shop/cart")

return super().cart_update(
product_id=product_id,
add_qty=add_qty,
set_qty=set_qty,
product_custom_attribute_values=product_custom_attribute_values,
no_variant_attribute_values=no_variant_attribute_values,
express=express,
**kwargs
)

@http.route(["/shop/cart"], type="http", auth="public", website=True, sitemap=False)
def cart(self, access_token=None, revive="", **post):
warning = request.session["website_sale_cart_warning"]
del request.session["website_sale_cart_warning"]
response = super().cart(access_token=access_token, revive=revive, **post)
response.qcontext["website_sale_cart_warning"] = warning
return response
28 changes: 18 additions & 10 deletions website_sale_restrict_sepa_dd/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,45 @@ def _compute_only_sepa_dd_payment(self):
order.order_line.mapped("product_id").mapped("only_sepa_dd_payment")
)

def _cart_update(self, product_id, line_id=None, add_qty=0, set_qty=0, **kwargs):
"""Prevent incompatible product to be added to the cart."""
def check_product_compatibility(self, product_id):
self.ensure_one()
product = self.env["product.product"].browse(product_id).exists()
warning = ""
only_sepa_dd_product = self.order_line.mapped("product_id").filtered(
lambda p: p.only_sepa_dd_payment
)
allow_sepa_dd_payment = all(
self.order_line.mapped("product_id").mapped("allow_sepa_dd_payment")
)
if product:
only_sepa_dd_product = self.order_line.mapped("product_id").filtered(
lambda p: p.only_sepa_dd_payment
)
allow_sepa_dd_payment = all(
self.order_line.mapped("product_id").mapped("allow_sepa_dd_payment")
)
if only_sepa_dd_product and not product.allow_sepa_dd_payment:
# This product cannot be added
add_qty, set_qty = None, 0
warning = _(
f"Product {product.name} cannot be added to cart "
"because product(s) : "
f"{', '.join(p.name for p in only_sepa_dd_product)} "
"must be payed by SEPA Direct Debit and product "
f"{product.name} cannot be payed by such method."
"Please process this order first, or clear "
f"the order."
)
elif not allow_sepa_dd_payment and product.only_sepa_dd_payment:
add_qty, set_qty = None, 0
warning = _(
f"Product {product.name} cannot be added to cart "
"because other products in the cart does not allow "
"SEPA Direct Debit as payment method and "
f"{product.name} must be payed with such a method."
"Please process this order first, or clear it."
)
return warning

def _cart_update(self, product_id, line_id=None, add_qty=0, set_qty=0, **kwargs):
"""Prevent incompatible product to be added to the cart."""
self.ensure_one()
warning = self.check_product_compatibility(product_id)
if warning:
add_qty = None
set_qty = 0
values = super()._cart_update(
product_id=product_id,
line_id=line_id,
Expand Down
18 changes: 6 additions & 12 deletions website_sale_restrict_sepa_dd/views/templates.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<template id="product" inherit_id="website_sale.product">
<xpath expr="//div[@id='o_wsale_cta_wrapper']" position="attributes">
<attribute name="t-if">not product.only_sepa_dd_payment</attribute>
</xpath>
<xpath expr="//div[@id='o_wsale_cta_wrapper']" position="after">
<template id="cart_restrict_sepa_dd" inherit_id="website_sale.cart">
<xpath expr="//t[@t-call='website_sale.cart_lines']" position="before">
<div
t-else=""
id="o_subscription_wrapper"
class="d-flex flex-wrap align-items-center"
t-if="website_sale_cart_warning"
class="mt8 mb8 alert alert-danger"
role="alert"
>
<a class="btn btn-lg btn-primary" href="/subscription/process">
<i class="fa fa-shopping-cart me-2" />
<span style="font-weight: bold">SUBSCRIBE</span>
</a>
<t t-out="website_sale_cart_warning" />
</div>
</xpath>
</template>
Expand Down

0 comments on commit aef94c7

Please sign in to comment.