diff --git a/addons/website_sale/static/src/js/website_sale.js b/addons/website_sale/static/src/js/website_sale.js index cdd0dcf56ae..6d25860a9af 100644 --- a/addons/website_sale/static/src/js/website_sale.js +++ b/addons/website_sale/static/src/js/website_sale.js @@ -372,21 +372,11 @@ publicWidget.registry.WebsiteSale = publicWidget.Widget.extend(VariantMixin, car if (!data.cart_quantity) { return window.location = '/shop/cart'; } - wSaleUtils.updateCartNavBar(data); $input.val(data.quantity); $('.js_quantity[data-line-id='+line_id+']').val(data.quantity).text(data.quantity); - if (data.warning) { - var cart_alert = $('.oe_cart').parent().find('#data_warning'); - if (cart_alert.length === 0) { - $('.oe_cart').prepend(''); - } - else { - cart_alert.html(' ' + data.warning); - } - $input.val(data.quantity); - } + wSaleUtils.updateCartNavBar(data); + wSaleUtils.showWarning(data.warning); }); }, /** diff --git a/addons/website_sale/static/src/js/website_sale_utils.js b/addons/website_sale/static/src/js/website_sale_utils.js index ba5cdc842c7..75f8ae80a4b 100644 --- a/addons/website_sale/static/src/js/website_sale_utils.js +++ b/addons/website_sale/static/src/js/website_sale_utils.js @@ -95,22 +95,48 @@ function animateClone($cart, $elem, offsetTop, offsetLeft) { * @param {Object} data */ function updateCartNavBar(data) { - var $qtyNavBar = $(".my_cart_quantity"); - _.each($qtyNavBar, function (qty) { - var $qty = $(qty); - $qty.parents('li:first').removeClass('d-none'); - $qty.addClass('o_mycart_zoom_animation').delay(300).queue(function () { - $(this).text(data.cart_quantity); - $(this).removeClass("o_mycart_zoom_animation").dequeue(); + $(".my_cart_quantity") + .parents('li.o_wsale_my_cart').removeClass('d-none').end() + .addClass('o_mycart_zoom_animation').delay(300) + .queue(function () { + $(this) + .toggleClass('fa fa-warning', !data.cart_quantity) + .attr('title', data.warning) + .text(data.cart_quantity || '') + .removeClass('o_mycart_zoom_animation') + .dequeue(); }); - }); + $(".js_cart_lines").first().before(data['website_sale.cart_lines']).end().remove(); $(".js_cart_summary").first().before(data['website_sale.short_cart_summary']).end().remove(); } +/** + * Displays `message` in an alert box at the top of the page if it's a + * non-empty string. + * + * @param {string | null} message + */ +function showWarning(message) { + if (!message) { + return; + } + var $page = $('.oe_website_sale'); + var cart_alert = $page.children('#data_warning'); + if (!cart_alert.length) { + cart_alert = $( + '').prependTo($page); + } + cart_alert.children('span:last-child').text(message); +} + return { animateClone: animateClone, updateCartNavBar: updateCartNavBar, cartHandlerMixin: cartHandlerMixin, + showWarning: showWarning, }; }); diff --git a/addons/website_sale_wishlist/static/src/js/website_sale_wishlist.js b/addons/website_sale_wishlist/static/src/js/website_sale_wishlist.js index dfaabb010ec..699cb590ee4 100644 --- a/addons/website_sale_wishlist/static/src/js/website_sale_wishlist.js +++ b/addons/website_sale_wishlist/static/src/js/website_sale_wishlist.js @@ -188,16 +188,10 @@ publicWidget.registry.ProductWishlist = publicWidget.Widget.extend(VariantMixin, return this._rpc({ route: "/shop/cart/update_json", params: this._getCartUpdateJsonParams(productID, qty), - }).then(function (resp) { - if (resp.warning) { - if (! $('#data_warning').length) { - $('.wishlist-section').prepend(''); - } - var cart_alert = $('.wishlist-section').parent().find('#data_warning'); - cart_alert.html(' ' + resp.warning); - } - sessionStorage.setItem('website_sale_cart_quantity', resp.cart_quantity); - $('.my_cart_quantity').html(resp.cart_quantity || ' '); + }).then(function (data) { + sessionStorage.setItem('website_sale_cart_quantity', data.cart_quantity); + wSaleUtils.updateCartNavBar(data); + wSaleUtils.showWarning(data.warning); }); }, /**