|
|
|
@ -4,7 +4,6 @@ import publicWidget from "@web/legacy/js/public/public_widget"; |
|
|
|
import wSaleUtils from "@website_sale/js/website_sale_utils"; |
|
|
|
import { Component } from "@odoo/owl"; |
|
|
|
|
|
|
|
|
|
|
|
publicWidget.registry.WebsiteSale.include({ |
|
|
|
/** |
|
|
|
* This module extends the website_sale module to support decimal quantity input for adding products to the cart. |
|
|
|
@ -13,29 +12,34 @@ publicWidget.registry.WebsiteSale.include({ |
|
|
|
*/ |
|
|
|
_onClickAddCartJSON(ev) { |
|
|
|
ev.preventDefault(); |
|
|
|
var $link = $(ev.currentTarget); |
|
|
|
var $input = $link.closest('.input-group').find("input"); |
|
|
|
var min = parseFloat($input.data("min") || 0); |
|
|
|
var max = parseFloat($input.data("max") || Infinity); |
|
|
|
var previousQty = parseFloat($input.val() || 0, 10); |
|
|
|
var quantity = ($link.has(".fa-minus").length ? -0.1 : 0.1) + previousQty; |
|
|
|
var newQt = quantity > min ? (quantity < max ? quantity : max) : min; |
|
|
|
if (newQt !== previousQty) { |
|
|
|
var newQty = newQt.toFixed(1); |
|
|
|
$input.val(newQty).trigger('change'); |
|
|
|
} |
|
|
|
newQty = newQt.toFixed(1); |
|
|
|
const $link = $(ev.currentTarget); |
|
|
|
const $input = $link.closest('.input-group').find("input"); |
|
|
|
const min = parseFloat($input.attr("min")) || 0.01; |
|
|
|
const max = parseFloat($input.attr("max")) || Infinity; |
|
|
|
const step = parseFloat($input.attr("step")) || 0.1; |
|
|
|
const previousQty = parseFloat($input.val()) || 0; |
|
|
|
|
|
|
|
const isMinus = $link.find(".fa-minus").length > 0; |
|
|
|
|
|
|
|
let newQty = isMinus ? previousQty - step : previousQty + step; |
|
|
|
|
|
|
|
if (newQty < min) newQty = min; |
|
|
|
if (newQty > max) newQty = max; |
|
|
|
|
|
|
|
const precision = step < 1 ? step.toString().split('.')[1]?.length || 1 : 0; |
|
|
|
newQty = parseFloat(newQty.toFixed(precision)); |
|
|
|
$input.val(newQty).trigger('change'); |
|
|
|
return false; |
|
|
|
}, |
|
|
|
|
|
|
|
/** Override the function _changeCartQuantity for changing the cart quantity **/ |
|
|
|
_changeCartQuantity: function ($input, value, $dom_optional, line_id, productIDs) { |
|
|
|
_changeCartQuantity($input, value, $dom_optional, line_id, productIDs) { |
|
|
|
$($dom_optional).toArray().forEach((elem) => { |
|
|
|
|
|
|
|
$(elem).find('.js_quantity').text(value); |
|
|
|
productIDs.push($(elem).find('span[data-product-id]').data('product-id')); |
|
|
|
}); |
|
|
|
$input.data('update_change', true); |
|
|
|
|
|
|
|
$input.data('update_change', true); |
|
|
|
this.rpc("/shop/cart/update_json", { |
|
|
|
line_id: line_id, |
|
|
|
product_id: parseInt($input.data('product-id'), 10), |
|
|
|
@ -43,41 +47,64 @@ publicWidget.registry.WebsiteSale.include({ |
|
|
|
display: true, |
|
|
|
}).then((data) => { |
|
|
|
$input.data('update_change', false); |
|
|
|
var check_value = parseFloat($input.val()); |
|
|
|
if (isNaN(check_value)) { |
|
|
|
check_value = 1; |
|
|
|
} |
|
|
|
let check_value = parseFloat($input.val()); |
|
|
|
if (isNaN(check_value)) check_value = value; |
|
|
|
|
|
|
|
if (value !== check_value) { |
|
|
|
$input.trigger('change'); |
|
|
|
return; |
|
|
|
} |
|
|
|
if (!data.cart_quantity) { |
|
|
|
return window.location = '/shop/cart'; |
|
|
|
return (window.location = '/shop/cart'); |
|
|
|
} |
|
|
|
|
|
|
|
$input.val(data.quantity); |
|
|
|
$('.js_quantity[data-line-id='+line_id+']').val(data.quantity).text(data.quantity); |
|
|
|
$('.js_quantity[data-line-id=' + line_id + ']').val(data.quantity).text(data.quantity); |
|
|
|
|
|
|
|
wSaleUtils.updateCartNavBar(data); |
|
|
|
wSaleUtils.showWarning(data.notification_info.warning); |
|
|
|
|
|
|
|
// Propagating the change to the express checkout forms
|
|
|
|
Component.env.bus.trigger('cart_amount_changed', [data.amount, data.minor_amount]); |
|
|
|
}); |
|
|
|
}, |
|
|
|
/** Override the function _onChangeCartQuantity **/ |
|
|
|
_onChangeCartQuantity: function (ev) { |
|
|
|
|
|
|
|
/** Override the function _onChangeCartQuantity **/ |
|
|
|
_onChangeCartQuantity(ev) { |
|
|
|
ev.preventDefault(); |
|
|
|
var $input = $(ev.currentTarget); |
|
|
|
if ($input.data('update_change')) { |
|
|
|
return; |
|
|
|
} |
|
|
|
var value = parseFloat($input.val() || 0, 10); |
|
|
|
const $input = $(ev.currentTarget); |
|
|
|
if ($input.data('update_change')) return; |
|
|
|
|
|
|
|
let value = parseFloat($input.val()); |
|
|
|
if (isNaN(value)) { |
|
|
|
value = 1; |
|
|
|
value = parseFloat($input.attr("min")) || 0.01; |
|
|
|
} else if (value < 0) { |
|
|
|
value = parseFloat($input.attr("min")) || 0.01; |
|
|
|
} |
|
|
|
var $dom = $input.closest('tr'); |
|
|
|
var $dom_optional = $dom.nextUntil(':not(.optional_product.info)'); |
|
|
|
var line_id = parseInt($input.data('line-id'), 10); |
|
|
|
var productIDs = [parseInt($input.data('product-id'), 10)]; |
|
|
|
const $dom = $input.closest('tr'); |
|
|
|
const $dom_optional = $dom.nextUntil(':not(.optional_product.info)'); |
|
|
|
const line_id = parseInt($input.data('line-id'), 10); |
|
|
|
const productIDs = [parseInt($input.data('product-id'), 10)]; |
|
|
|
|
|
|
|
this._changeCartQuantity($input, value, $dom_optional, line_id, productIDs); |
|
|
|
}, |
|
|
|
|
|
|
|
async _onClickDelete(ev) { |
|
|
|
ev.preventDefault(); |
|
|
|
ev.stopPropagation(); |
|
|
|
|
|
|
|
const tr = ev.currentTarget.closest('tr'); |
|
|
|
const line_id = parseInt(tr.dataset.lineId); |
|
|
|
const $input = $(tr).find('.js_quantity'); |
|
|
|
|
|
|
|
if (!line_id || !$input.length) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
await this._changeCartQuantity($input, 0, [], line_id, []); |
|
|
|
|
|
|
|
$(tr).fadeOut(300, () => $(tr).remove()); |
|
|
|
setTimeout(() => window.location.reload(), 400); |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|