Skip to content

Commit 2dc9e07

Browse files
[IMP] website_sale_insurance_caser: move caser insurance modal to cart checkout button
1 parent c79526b commit 2dc9e07

3 files changed

Lines changed: 64 additions & 29 deletions

File tree

website_sale_insurance_caser/README.rst

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ purchase insurance for their products directly from the online store.
3333

3434
This module extends **sale_insurance_caser** by adding:
3535

36-
- Checkbox on product page to add insurance
37-
- Interactive modal at checkout offering insurance for uncovered
38-
products
39-
- Visual badges identifying insurance lines in the cart
40-
- Automatic synchronization between cart and insurance lines
36+
- Checkbox on product page to add insurance
37+
- Interactive modal at checkout offering insurance for uncovered
38+
products
39+
- Visual badges identifying insurance lines in the cart
40+
- Automatic synchronization between cart and insurance lines
4141

4242
**Table of contents**
4343

@@ -49,16 +49,16 @@ Configuration
4949

5050
1. **Configure the base module** ``sale_insurance_caser``:
5151

52-
- Define price ranges (``caser.price.range``)
53-
- Configure insurance products
54-
- Set up Caser API credentials
52+
- Define price ranges (``caser.price.range``)
53+
- Configure insurance products
54+
- Set up Caser API credentials
5555

5656
2. **Enable insurance on eCommerce products**:
5757

58-
- Go to *Inventory > Products > Products*
59-
- Open a product you want to offer with insurance
60-
- In the **Sales** tab, check the **Caser Insurance in eCommerce**
61-
checkbox
58+
- Go to *Inventory > Products > Products*
59+
- Open a product you want to offer with insurance
60+
- In the **Sales** tab, check the **Caser Insurance in eCommerce**
61+
checkbox
6262

6363
Usage
6464
=====
@@ -83,9 +83,9 @@ Usage
8383
uninsured products
8484
7. The modal displays:
8585

86-
- Product image and name
87-
- Insurance price per unit and total
88-
- Toggle switches to select which products to insure
86+
- Product image and name
87+
- Insurance price per unit and total
88+
- Toggle switches to select which products to insure
8989

9090
8. Upon confirmation, page reloads with new insurance lines added
9191

@@ -110,9 +110,9 @@ Authors
110110
Contributors
111111
------------
112112

113-
- `Tecnativa <https://www.tecnativa.com>`__:
113+
- `Tecnativa <https://www.tecnativa.com>`__:
114114

115-
- Juan Carlos Oñate
115+
- Juan Carlos Oñate
116116

117117
Maintainers
118118
-----------

website_sale_insurance_caser/static/src/js/checkout_insurance.esm.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,45 @@ import {rpc} from "@web/core/network/rpc";
44
publicWidget.registry.caserInsuranceCheckout = publicWidget.Widget.extend({
55
selector: ".oe_website_sale",
66
events: {
7+
"click a[name='website_sale_main_button']": "_onCheckoutClick",
78
"click #addInsuranceBtn": "_onAddInsurance",
9+
"click #skipInsuranceBtn": "_onSkipInsurance",
10+
"click #closeInsuranceModal": "_onSkipInsurance",
11+
"click #toggleAllInsurance": "_onToggleAll",
812
},
913

10-
async start() {
11-
await this._super(...arguments);
12-
if (window.location.pathname.includes("/shop/payment")) {
13-
await this._checkUninsuredProducts();
14-
}
14+
start() {
15+
this._checkoutHref = this.el
16+
.querySelector("a[name='website_sale_main_button']")
17+
?.getAttribute("href");
18+
return this._super(...arguments);
1519
},
1620

17-
async _checkUninsuredProducts() {
21+
async _onCheckoutClick(ev) {
22+
if (!window.location.pathname.includes("/shop/cart")) {
23+
return;
24+
}
25+
ev.preventDefault();
1826
const data = await rpc("/shop/caser_insurance/get_uninsured_products");
1927
if (data.products?.length) {
2028
this._showInsuranceModal(data.products);
29+
} else {
30+
window.location.href = this._checkoutHref;
2131
}
2232
},
2333

2434
_showInsuranceModal(products) {
2535
const $list = this.$("#uninsured_products_list");
2636
$list.empty();
37+
const hasMultiUnit = products.some((p) => p.quantity > 1);
2738

2839
products.forEach((product) => {
40+
const checked = product.quantity === 1 ? "checked" : "";
2941
for (let i = 0; i < product.quantity; i++) {
3042
$list.append(`
3143
<div class="d-flex align-items-center gap-3 py-2 border-bottom">
3244
<input class="form-check-input flex-shrink-0 insurance-checkbox"
33-
type="checkbox"
45+
type="checkbox" ${checked}
3446
data-line-id="${product.line_id}"
3547
id="ins_${product.line_id}_${i}"/>
3648
<img src="${product.image_url}" alt="${product.product_name}"
@@ -44,9 +56,27 @@ publicWidget.registry.caserInsuranceCheckout = publicWidget.Widget.extend({
4456
}
4557
});
4658

59+
const $toggle = this.$("#toggleAllInsurance");
60+
if (hasMultiUnit) {
61+
$toggle.removeClass("d-none");
62+
} else {
63+
$toggle.addClass("d-none");
64+
}
65+
4766
this.$("#caserInsuranceModal").modal("show");
4867
},
4968

69+
_onToggleAll() {
70+
const $checkboxes = this.$(".insurance-checkbox");
71+
const allChecked = $checkboxes.length === $checkboxes.filter(":checked").length;
72+
$checkboxes.prop("checked", !allChecked);
73+
},
74+
75+
_onSkipInsurance() {
76+
this.$("#caserInsuranceModal").modal("hide");
77+
window.location.href = this._checkoutHref;
78+
},
79+
5080
async _onAddInsurance(ev) {
5181
ev.preventDefault();
5282
const lineInsurances = {};

website_sale_insurance_caser/views/templates.xml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@
9191
</t>
9292
</xpath>
9393
</template>
94-
<template id="caser_insurance_checkout_modal" inherit_id="website_sale.payment">
95-
<xpath expr="//div[@id='payment_method']" position="before">
94+
<template id="caser_insurance_checkout_modal" inherit_id="website_sale.cart">
95+
<xpath expr="//t[@t-call='website_sale.cart_lines']" position="after">
9696
<div class="modal fade" id="caserInsuranceModal" tabindex="-1">
9797
<div class="modal-dialog modal-lg">
9898
<div class="modal-content">
@@ -102,8 +102,8 @@
102102
</h5>
103103
<button
104104
type="button"
105-
class="btn-close btn-close-white"
106-
data-bs-dismiss="modal"
105+
class="btn-close"
106+
id="closeInsuranceModal"
107107
/>
108108
</div>
109109
<div class="modal-body">
@@ -113,10 +113,15 @@
113113
<div id="uninsured_products_list" />
114114
</div>
115115
<div class="modal-footer">
116+
<button
117+
type="button"
118+
class="btn btn-link me-auto d-none"
119+
id="toggleAllInsurance"
120+
>Marcar / Desmarcar todos</button>
116121
<button
117122
type="button"
118123
class="btn btn-secondary"
119-
data-bs-dismiss="modal"
124+
id="skipInsuranceBtn"
120125
>No, thanks</button>
121126
<button
122127
type="button"

0 commit comments

Comments
 (0)