47 lines
1.5 KiB
JavaScript

/** @odoo-module **/
import publicWidget from "@web/legacy/js/public/public_widget";
publicWidget.registry.Dine360WhatsAppButton = publicWidget.Widget.extend({
selector: '.dine360-wa-product-wrap',
start: function () {
this._super.apply(this, arguments);
this._bindEvents();
},
_bindEvents: function () {
const self = this;
// Listen to variant combination changes to dynamically update WhatsApp message if desired
$(document).on('change', 'ul.js_add_cart_variants input, input[name="add_qty"]', function () {
self._updateWhatsAppLink();
});
},
_updateWhatsAppLink: function () {
const $btn = this.$el.find('.dine360-wa-product-btn');
if (!$btn.length) return;
const currentHref = $btn.attr('href');
if (!currentHref || !currentHref.includes('text=')) return;
const qty = $('input[name="add_qty"]').val();
if (qty && parseInt(qty) > 1) {
// Append quantity note to inquiry if not already present
try {
const url = new URL(currentHref);
let text = url.searchParams.get('text') || '';
if (!text.includes('Qty:')) {
text += ` (Qty: ${qty})`;
url.searchParams.set('text', text);
$btn.attr('href', url.toString());
}
} catch (e) {
// Fallback for older browsers
}
}
},
});
export default publicWidget.registry.Dine360WhatsAppButton;