diff --git a/app/assets/javascripts/alchemy/solidus/admin/variant_select.js b/app/assets/javascripts/alchemy/solidus/admin/variant_select.js index 160452a..acbdc73 100644 --- a/app/assets/javascripts/alchemy/solidus/admin/variant_select.js +++ b/app/assets/javascripts/alchemy/solidus/admin/variant_select.js @@ -1,32 +1,55 @@ //= require alchemy/solidus/admin/select2_config -$.fn.alchemyVariantSelect = function(options) { - var config = Alchemy.Solidus.getSelect2Config(options) +$.fn.alchemyVariantSelect = function (options) { + const config = Alchemy.Solidus.getSelect2Config(options) - this.select2($.extend(true, config, { - ajax: { - data: function(term, page) { - return { - q: $.extend({ - product_name_or_sku_cont: term - }, options.query_params), - page: page - } + function formatSelection(variant) { + return variant.options_text + ? `${variant.name} - ${variant.options_text}` + : variant.name + } + + function formatResult(variant, _el, query) { + const matchTerm = new RegExp(query.term, "gi") + const formatMatch = (match) => `${match}` + const name = variant.name.replace(matchTerm, formatMatch) + const sku = variant.sku.replace(matchTerm, formatMatch) + return ` +
+
+ ${name} +
+
+ ${variant.options_text} + ${sku} +
+
+ ` + } + + this.select2( + $.extend(true, config, { + ajax: { + data: function (term, page) { + return { + q: $.extend( + { + product_name_or_sku_cont: term, + }, + options.query_params + ), + page: page, + } + }, + results: function (data, page) { + return { + results: data.variants, + more: page * data.per_page < data.total_count, + } + }, }, - results: function(data, page) { - return { - results: data.variants.map(function(variant) { - return { - id: variant.id, - text: variant.frontend_display - } - }), - more: page * data.per_page < data.total_count - } - } - }, - formatSelection: function(variant) { - return variant.text || variant.frontend_display - } - })) + formatSelection, + formatResult, + }) + ) } diff --git a/app/views/alchemy/ingredients/_spree_variant_editor.html.erb b/app/views/alchemy/ingredients/_spree_variant_editor.html.erb index 5a4ebf4..b87d227 100644 --- a/app/views/alchemy/ingredients/_spree_variant_editor.html.erb +++ b/app/views/alchemy/ingredients/_spree_variant_editor.html.erb @@ -10,6 +10,25 @@ <% end %> <% end %> + +