From 5a23a935fe720ba23ab2e336d5cb9e844dbe7b0b Mon Sep 17 00:00:00 2001 From: "Nasreddin (bon)" Date: Tue, 5 Jan 2021 13:56:06 +0000 Subject: [PATCH] [FIX] website,website_sale: Dynamic products isn't visible if category deleted Issue - Install "Ecommerce" app - Go to 'Shop' page and edit it - Add a "Dynamic Products" block - Click on it to edit it : - Select any template - Select `Desks` as product category - Save - Go to "Website -> Configuration -> eCommerce Categories" - Delete the "Desks" category - Go back to Shop page Error : "Record does not exist or has been deleted". Cause The search domain is looking for child categories of dataset.productCategoryId (who does not exist anymore in the above case). `child_of` do a browse() to fetch the records, and therefore generate an error if category not found. Solution Check if dataset.productCategoryId exist before updating domain. opw-2416231 closes odoo/odoo#64157 X-original-commit: 0de069b8ca9fb005ba5b076984f5677de25889ee Signed-off-by: LTU-Odoo Signed-off-by: bon-odoo --- .../src/snippets/s_dynamic_snippet/000.js | 6 ++--- .../s_dynamic_snippet_products/000.js | 26 ++++++++++++++++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/addons/website/static/src/snippets/s_dynamic_snippet/000.js b/addons/website/static/src/snippets/s_dynamic_snippet/000.js index 71b551738b5..46b10ea0dc9 100644 --- a/addons/website/static/src/snippets/s_dynamic_snippet/000.js +++ b/addons/website/static/src/snippets/s_dynamic_snippet/000.js @@ -94,14 +94,14 @@ const DynamicSnippet = publicWidget.Widget.extend({ * domain if needed. * @private */ - _getSearchDomain: function () { + _getSearchDomain: async function () { return []; }, /** * Fetches the data. * @private */ - _fetchData: function () { + _fetchData: async function () { if (this._isConfigComplete()) { return this._rpc( { @@ -110,7 +110,7 @@ const DynamicSnippet = publicWidget.Widget.extend({ 'filter_id': parseInt(this.$el.get(0).dataset.filterId), 'template_key': this.$el.get(0).dataset.templateKey, 'limit': parseInt(this.$el.get(0).dataset.numberOfRecords), - 'search_domain': this._getSearchDomain() + 'search_domain': await this._getSearchDomain() }, }) .then( diff --git a/addons/website_sale/static/src/snippets/s_dynamic_snippet_products/000.js b/addons/website_sale/static/src/snippets/s_dynamic_snippet_products/000.js index 73b7d3abb02..44225f83928 100644 --- a/addons/website_sale/static/src/snippets/s_dynamic_snippet_products/000.js +++ b/addons/website_sale/static/src/snippets/s_dynamic_snippet_products/000.js @@ -13,13 +13,25 @@ const DynamicSnippetProducts = DynamicSnippetCarousel.extend({ // Private //-------------------------------------------------------------------------- + /** + * Check product category exist. + * @private + * @returns {Promise} + */ + _checkCategoryExist: async function (productCategoryId) { + return this._rpc({ + model: 'product.public.category', + method: 'search_count', + args: [[['id', '=', productCategoryId]]], + }).then(nb => nb > 0); + }, /** * Method to be overridden in child components if additional configuration elements * are required in order to fetch data. * @override * @private */ - _isConfigComplete: function() { + _isConfigComplete: function () { return this._super.apply(this, arguments) && this.$el.get(0).dataset.productCategoryId !== undefined; }, /** @@ -28,9 +40,15 @@ const DynamicSnippetProducts = DynamicSnippetCarousel.extend({ * @override * @private */ - _getSearchDomain: function () { - const searchDomain = this._super.apply(this, arguments); - searchDomain.push(['public_categ_ids', 'child_of', parseInt(this.$el.get(0).dataset.productCategoryId)]); + _getSearchDomain: async function () { + const searchDomain = await this._super.apply(this, arguments); + const productCategoryId = parseInt(this.$el.get(0).dataset.productCategoryId); + const categoryExist = await this._checkCategoryExist(productCategoryId); + if (categoryExist) { + searchDomain.push(['public_categ_ids', 'child_of', productCategoryId]); + } else { + searchDomain.push([0, '=', 1]); + } return searchDomain; },