From fd3c1c1343ef16fe34fa2e7961d192e1c244cfd5 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Tue, 13 Jan 2026 02:13:51 +0500 Subject: [PATCH] Add "None" option to tags filter in product search --- app/controllers/admin/products_v3_controller.rb | 14 ++++++++------ app/views/admin/products_v3/_filters.html.haml | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index db600e2c5b..95083ad421 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -7,6 +7,7 @@ module Admin before_action :init_filters_params before_action :init_pagination_params + before_action :init_none_tag def index fetch_products @@ -242,11 +243,8 @@ module Admin def apply_tags_filter(base_query) return base_query if @tags.blank? - tags = Array(@tags) - none_key = I18n.t('admin.products_v3.filters.tags.none') - - has_none = tags.include?(none_key) - tag_names = tags.reject { |t| t == none_key } + tag_names = Array(@tags).dup + has_none_tag = (tag_names.delete(@none_tag_value) == @none_tag_value) queries = [] @@ -260,7 +258,7 @@ module Admin queries << base_query.where(id: tagged_product_ids) end - if has_none + if has_none_tag # Products where no variants have any tags tagged_product_ids = Spree::Variant .joins(:taggings) @@ -333,6 +331,10 @@ module Admin t('.error') end end + + def init_none_tag + @none_tag_value = "without_any_tag" + end end end # rubocop:enable Metrics/ClassLength diff --git a/app/views/admin/products_v3/_filters.html.haml b/app/views/admin/products_v3/_filters.html.haml index a3e30e53b1..68e0c88ebb 100644 --- a/app/views/admin/products_v3/_filters.html.haml +++ b/app/views/admin/products_v3/_filters.html.haml @@ -23,7 +23,7 @@ - select_tag_options = { class: "fullwidth", multiple: true , data: { controller: "tom-select", "tom-select-placeholder-value": t(".select_tag"), "tom-select-options-value": '{ "maxItems": 5 , "plugins": { "remove_button": {} , "no_active_items": {}, "checkbox_options": { "checkedClassNames": ["ts-checked"], "uncheckedClassNames": ["ts-unchecked"] } } }' } } - = select_tag :tags_name_in, options_for_select(available_tags.unshift(t('.tags.none')), tags), select_tag_options + = select_tag :tags_name_in, options_for_select(available_tags.unshift([t('.tags.none'), @none_tag_value]), tags), select_tag_options .submit .search-button = button_tag t(".search"), class: "secondary icon-search relaxed", name: nil