Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Open source Taxons and Taxonomies management in admin (#12248)

Łukasz Adamczykf7c236c22063
diff --git a/admin/app/controllers/spree/admin/classifications_controller.rb b/admin/app/controllers/spree/admin/classifications_controller.rb
new file mode 100644
index 0000000000..98e08f60af
--- /dev/null
+++ b/admin/app/controllers/spree/admin/classifications_controller.rb
@@ -0,0 +1,74 @@
+module Spree
+ module Admin
+ class ClassificationsController < ResourceController
+ belongs_to 'spree/taxon', find_by: :id
+
+ layout 'turbo_rails/frame'
+
+ before_action :load_sorted_classifications, only: %i[create index]
+
+ # create classifications in bulk
+ def create
+ products = current_store.products.accessible_by(current_ability, :update).where(id: params[:product_ids].compact.uniq)
+ Spree::Taxons::AddProducts.call(taxons: Spree::Taxon.where(id: parent.id), products: products)
+
+ parent.reload
+ @classifications = collection
+ end
+
+ private
+
+ def collection_url
+ spree.admin_taxon_classifications_path(@parent.id)
+ end
+
+ def update_turbo_stream_enabled?
+ true
+ end
+
+ def destroy_turbo_stream_enabled?
+ true
+ end
+
+ def collection
+ @collection ||= parent.
+ classifications.
+ joins(:product).
+ merge(current_store.products.not_archived).
+ includes(
+ :taxon,
+ product: {
+ variant_images: [],
+ master: [:images, :stock_items, :stock_locations],
+ variants: [:images, :stock_items, :stock_locations]
+ }
+ ).
+ accessible_by(current_ability)
+ end
+
+ def load_sorted_classifications
+ return @sorted_classifications if @sorted_classifications.present?
+
+ sort_params = { sort: taxon_sort_order_to_param(parent.sort_order) }
+ @sorted_classifications = Spree::Classifications::Sort.new(collection, current_currency, sort_params).call
+ end
+
+ def taxon_sort_order_to_param(sort_order)
+ return unless sort_order
+
+ sort_orders = {
+ 'manual' => 'manual',
+ 'best-selling' => '-best_selling',
+ 'name-a-z' => 'name',
+ 'name-z-a' => '-name',
+ 'price-low-to-high' => 'price',
+ 'price-high-to-low' => '-price',
+ 'newest-first' => '-available_on',
+ 'oldest-first' => 'available_on'
+ }
+
+ sort_orders.fetch(sort_order.to_s, 'manual')
+ end
+ end
+ end
+end
diff --git a/admin/app/controllers/spree/admin/taxonomies_controller.rb b/admin/app/controllers/spree/admin/taxonomies_controller.rb
new file mode 100644
index 0000000000..85aab0c284
--- /dev/null
+++ b/admin/app/controllers/spree/admin/taxonomies_controller.rb
@@ -0,0 +1,21 @@
+module Spree
+ module Admin
+ class TaxonomiesController < ResourceController
+ private
+
+ def collection
+ return @collection if @collection.present?
+
+ @collection = super
+
+ params[:q] ||= {}
+ @search = @collection.ransack(params[:q])
+ @collection = @search.result.all
+ end
+
+ def location_after_save
+ spree.admin_taxonomy_path(@taxonomy)
+ end
+ end
+ end
+end
diff --git a/admin/app/controllers/spree/admin/taxons_controller.rb b/admin/app/controllers/spree/admin/taxons_controller.rb
new file mode 100644
index 0000000000..728aaf1332
--- /dev/null
+++ b/admin/app/controllers/spree/admin/taxons_controller.rb
@@ -0,0 +1,94 @@
+module Spree
+ module Admin
+ class TaxonsController < ResourceController
+ belongs_to 'spree/taxonomy'
+
+ include Spree::Admin::TaxonsHelper
+ include Spree::Admin::TagsHelper
+
+ before_action :set_parent_permalink, only: :new
+ before_action :set_permalink_part, only: [:edit, :update]
+ before_action :set_permalink_params, only: [:update]
+
+ update.before :set_parent
+ update.before :set_position
+
+ before_action :load_form_data, only: [:show, :new, :create, :edit, :update]
+
+ def select_options
+ render json: taxons_options_json_array
+ end
+
+ def reposition
+ authorize! :update, @taxon
+
+ new_parent = @taxonomy.taxons.find(params.dig(:taxon, :new_parent_id))
+ new_index = params.dig(:taxon, :new_position_idx).to_i
+
+ if @taxon.move_to_child_with_index(new_parent, new_index)
+ head :ok
+ else
+ head :unprocessable_entity
+ end
+ end
+
+ private
+
+ def location_after_save
+ spree.edit_admin_taxonomy_taxon_path(@taxon.taxonomy_id, @taxon.id)
+ end
+
+ def destroy_turbo_stream_enabled?
+ true && !params[:force_redirect].to_b
+ end
+
+ def set_parent_permalink
+ @parent_permalink = @taxon.parent.present? ? @taxon.parent.permalink : @taxon.taxonomy.root.permalink
+ end
+
+ def set_permalink_part
+ @permalink_part = @taxon.permalink.split('/').last
+ @parent_permalink = @taxon.permalink.split('/')[0...-1].join('/')
+ end
+
+ def taxon_params
+ params.require(:taxon).permit(permitted_taxon_attributes)
+ end
+
+ def set_position
+ new_position = params[:taxon][:position]
+ @taxon.child_index = new_position.to_i if new_position
+ end
+
+ def set_parent
+ return unless params.dig(:taxon, :parent_id).present?
+
+ @taxon.parent = @taxonomy.taxons.find_by(id: params.dig(:taxon, :parent_id))
+ end
+
+ def set_permalink_params
+ params[:taxon][:permalink] = "#{@parent_permalink}/" + params[:permalink_part] if params.key? 'permalink_part'
+ end
+
+ def collection_url
+ spree.admin_taxonomy_path(@taxonomy)
+ end
+
+ # this is needed to set the parent_id for the newly initialized taxons
+ def build_resource
+ parent.send(controller_name).build(parent_id: params.dig(:taxon, :parent_id))
+ end
+
+ def load_form_data
+ @taxon_rules = Rails.application.config.spree.taxon_rules
+ @rule_types = @taxon_rules.map do |taxon_rule|
+ [Spree.t("admin.taxon_rules.#{taxon_rule.to_s.demodulize.underscore}"), taxon_rule.to_s]
+ end
+
+ @rule_match_policies = Spree::TaxonRule::MATCH_POLICIES.map do |policy|
+ [Spree.t("admin.taxon_rules.match_policies.#{policy}"), policy]
+ end
+ end
+ end
+ end
+end
diff --git a/admin/app/helpers/spree/admin/products_helper.rb b/admin/app/helpers/spree/admin/products_helper.rb
new file mode 100644
index 0000000000..a17d937c06
--- /dev/null
+++ b/admin/app/helpers/spree/admin/products_helper.rb
@@ -0,0 +1,113 @@
+module Spree
+ module Admin
+ module ProductsHelper
+ def display_admin_price(product_or_variant)
+ price_in_current_currency = product_or_variant.price_in(current_currency)
+
+ if price_in_current_currency.amount
+ price_in_current_currency.display_price_including_vat_for(current_price_options).to_html
+ end
+ end
+
+ def display_inventory(product_or_variant)
+ if product_or_variant.total_on_hand == BigDecimal::INFINITY
+ content_tag :span, class: 'text-muted' do
+ Spree.t(:not_tracking_inventory)
+ end
+ else
+ stock = if product_or_variant.total_on_hand.positive?
+ "#{product_or_variant.total_on_hand} #{Spree.t(:in_stock).downcase}"
+ else
+ content_tag(:span, "0 #{Spree.t(:in_stock).downcase}", class: 'text-danger')
+ end
+ if product_or_variant.respond_to?(:variants) && product_or_variant.variants.size.positive?
+ stock += " #{Spree.t(:for)} #{product_or_variant.variants.size} #{Spree.t(:variants).downcase}"
+ end
+ stock
+ end
+ end
+
+ # we use this method to fetch images for media gallery on product form
+ # for new products we use session to store temporary assets not tied to any variant/product (yet)
+ def media_form_assets(variant)
+ if variant&.persisted?
+ variant.images
+ elsif session[:uploaded_asset_ids].present?
+ Spree::Image.accessible_by(current_ability, :manage).where(id: session[:uploaded_asset_ids].split(','))
+ end
+ end
+
+ def product_status(product)
+ if product.deleted?
+ content_tag(:span, 'Deleted', class: 'badge badge-danger')
+ else
+ content_tag(:span, class: "badge badge-#{product.status}") do
+ if product.active?
+ icon('check')
+ else
+ ''
+ end + available_status(product)
+ end.html_safe
+ end
+ end
+
+ def product_currencies(product)
+ product_currencies = product.prices_including_master.map(&:currency).compact.uniq.map do |currency|
+ ::Money::Currency.find(currency)
+ end
+
+ (supported_currencies_list + product_currencies).compact.uniq
+ end
+
+ # will return a human readable string
+ def available_status(product)
+ return Spree.t('admin.products.draft') if product.draft?
+ return Spree.t('admin.products.active') if product.available?
+ return Spree.t('admin.products.archived') if product.archived?
+ return Spree.t('admin.products.paused') if product.paused?
+ return Spree.t('admin.products.deleted') if product.deleted?
+ return ''
+ end
+
+ def available_status_options(product)
+ options = ['draft', 'active', 'archived']
+ options.delete('active') if cannot?(:activate, product)
+ options.map { |status| [Spree.t(status), status] }
+ end
+
+ def map_categories(product)
+ category_tree = ['n/a', 'n/a']
+
+ category = product.main_taxon&.pretty_name.to_s
+ return category_tree if category.blank?
+
+ category.split('->').map(&:strip).map.with_index { |c, index| category_tree[index] = c }
+
+ category_tree
+ end
+
+ def product_filter_status_dropdown_value
+ case params[:q][:status_eq]
+ when 'active'
+ Spree.t('admin.products.active')
+ when 'draft'
+ Spree.t('admin.products.draft')
+ when 'archived'
+ Spree.t('admin.products.archived')
+ else
+ 'All statuses'
+ end
+ end
+
+ def product_filter_stock_dropdown_value
+ if params[:q][:in_stock_items] == '1'
+ Spree.t('admin.products.in_stock')
+ elsif params[:q][:out_of_stock_items] == '1'
+ Spree.t('admin.products.out_of_stock')
+ else
+ 'Any stock'
+ end
+ end
+ end
+ end
+end
diff --git a/admin/app/helpers/spree/admin/tags_helper.rb b/admin/app/helpers/spree/admin/tags_helper.rb
new file mode 100644
index 0000000000..990c50b2ae
--- /dev/null
+++ b/admin/app/helpers/spree/admin/tags_helper.rb
@@ -0,0 +1,13 @@
+module Spree
+ module Admin
+ module TagsHelper
+ def tags_scope
+ @tags_scope ||= ActsAsTaggableOn::Tag.for_context(:tags)
+ end
+
+ def tags_json_array
+ @tags_json_array ||= tags_scope.pluck(:id, :name).map { |id, name| { id: id, name: name } }.as_json
+ end
+ end
+ end
+end
diff --git a/admin/app/helpers/spree/admin/taxons_helper.rb b/admin/app/helpers/spree/admin/taxons_helper.rb
new file mode 100644
index 0000000000..7ab5f87ed3
--- /dev/null
+++ b/admin/app/helpers/spree/admin/taxons_helper.rb
@@ -0,0 +1,22 @@
+module Spree
+ module Admin
+ module TaxonsHelper
+ def taxons_scope
+ @taxons_scope ||= current_store.taxons.where(automatic: false).where.not(parent_id: nil)
+ end
+
+ def taxons_options_json_array
+ taxons_scope.pluck(:id, :pretty_name).map { |id, pretty_name| { id: id, name: pretty_name } }.as_json
+ end
+
+ def taxon_sort_options_for_select
+ @taxon_sort_options_for_select ||= Spree::Taxon::SORT_ORDERS.map do |sort_order|
+ [
+ Spree.t("products_sort_options.#{sort_order.underscore}"),
+ sort_order
+ ]
+ end
+ end
+ end
+ end
+end
diff --git a/admin/app/views/active_storage/_upload_form.html.erb b/admin/app/views/active_storage/_upload_form.html.erb
new file mode 100644
index 0000000000..d50f144854
--- /dev/null
+++ b/admin/app/views/active_storage/_upload_form.html.erb
@@ -0,0 +1,65 @@
+<% width ||= 300 %>
+<% height ||= 300 %>
+<% crop ||= false unless defined?(crop) %>
+<% auto_submit ||= false %>
+<% can_delete ||= true %>
+<% css ||= '' %>
+
+<% if field_name && form %>
+ <div data-controller="active-storage-upload"
+ data-active-storage-upload-thumb-width-value="<%= width %>"
+ data-active-storage-upload-thumb-height-value="<%= height %>"
+ data-active-storage-upload-crop-value="<%= crop.presence || false %>"
+ data-active-storage-upload-auto-submit-value="<%= auto_submit %>"
+ data-active-storage-upload-field-name-value="<%= form.object_name %>[<%= field_name %>]"
+ data-active-storage-upload-allowed-file-types-value="<%= Rails.application.config.active_storage.web_image_content_types %>"
+ class="d-flex flex-column w-100 position-relative">
+ <div class="mb-3 rounded-lg overflow-hidden thumb-preview cursor-pointer bg-light"
+ data-action="click->active-storage-upload#open"
+ >
+ <% if form.object.send(field_name).attached? && form.object.send(field_name).variable? %>
+ <%= image_tag(
+ main_app.cdn_image_url(
+ form.object.send(field_name).variant(
+ spree_image_variant_options(
+ resize_to_fill: [width, (crop ? height : nil)]
+ )
+ )
+ ),
+ class: 'img-fluid rounded-lg',
+ data: { active_storage_upload_target: 'thumb' },
+ loading: :lazy
+ ) %>
+ <% else %>
+ <img data-active-storage-upload-target="thumb" class="img-fluid rounded-lg" style="display: none" />
+ <% end %>
+
+ <div class="d-flex justify-content-center align-items-center h-100 p-5 text-muted border border-dashed bg-gray-25 rounded-lg <%= css %>" data-active-storage-upload-target="placeholder" <% if form.object.send(field_name).attached? && form.object.send(field_name).variable? %>style="display: none !important"<% end %>>
+ <%= icon('camera', class: 'mr-2') %>
+ <%= Spree.t(:upload_image) %>
+
+ <% if crop %>
+ <span class="text-muted ml-2">
+ (Recommended size: <%= width %>x<%= height %>)
+ </span>
+ <% end %>
+ </div>
+ </div>
+
+ <div class="d-flex align-items-center" data-active-storage-upload-target="toolbar" <% unless form.object.send(field_name).attached? && form.object.send(field_name).variable? %>style="display: none !important"<% end %>>
+ <button type="button" class="btn btn-light" data-action="active-storage-upload#open">
+ <%= icon('camera') %>
+ Select file
+ </button>
+
+ <% if can_delete %>
+ <button type="button" class="btn btn-danger ml-auto" data-action="active-storage-upload#remove" data-turbo-confirm="<%= Spree.t(:are_you_sure) %>">
+ <%= icon('trash') %>
+
+ <%= Spree.t('actions.destroy') %>
+ </button>
+ <input type="hidden" name="remove_<%= field_name %>" data-active-storage-upload-target="remove" />
+ <% end %>
+ </div>
+ </div>
+<% end %>
diff --git a/admin/app/views/spree/admin/classifications/_classification.html.erb b/admin/app/views/spree/admin/classifications/_classification.html.erb
new file mode 100644
index 0000000000..a1f4f9b7e2
--- /dev/null
+++ b/admin/app/views/spree/admin/classifications/_classification.html.erb
@@ -0,0 +1,38 @@
+<div class="row mx-0 hover-light" id="<%= spree_dom_id(classification) %>" data-sortable-update-url="<%= spree.admin_taxon_classification_path(classification.taxon_id, classification, format: :turbo_stream) %>">
+ <% if classification.taxon.manual_sort_order? %>
+ <div class="col-1 text-center align-self-center">
+ <span class="rounded p-2 hover-gray with-tip move-handle cursor-grab">
+ <%= icon('grip-vertical', class: 'mr-0') %>
+ </span>
+ </div>
+ <% end %>
+
+ <div class="<%= classification.taxon.manual_sort_order? ? 'col-6' : 'col-7' %>">
+ <%= link_to spree.edit_admin_product_path(classification.product), data: { 'turbo-frame': '_top' }, class: "d-flex align-items-center text-dark #{classification.taxon.manual_sort_order? ? 'py-3' : 'p-3'}" do %>
+ <%= render 'spree/admin/shared/product_image', object: classification.product %>
+ <span class="ml-3">
+ <%= classification.product.name %>
+ </span>
+ <% end %>
+ </div>
+ <div class="col-2 text-center align-self-center">
+ <%= product_status(classification.product) %>
+ </div>
+ <div class="col-2 text-center align-self-center">
+ <% if classification.product.in_stock? %>
+ <span class="badge badge-success">
+ <%= Spree.t(:in_stock) %>
+ </span>
+ <% else %>
+ <span class="badge badge-pending">
+ <%= Spree.t(:out_of_stock) %>
+ </span>
+ <% end %>
+ </div>
+
+ <div class="col-1 text-center align-self-center">
+ <%= button_to spree.admin_taxon_classification_path(classification.taxon_id, classification), class: 'btn btn-danger btn-sm with-tip', method: :delete, data: { turbo_confirm: Spree.t(:are_you_sure) } do %>
+ <%= icon 'trash', class: 'mr-0', style: 'pointer-events: none' %>
+ <% end if classification.taxon.manual? && can?(:delete, classification) %>
+ </div>
+</div>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/classifications/create.turbo_stream.erb b/admin/app/views/spree/admin/classifications/create.turbo_stream.erb
new file mode 100644
index 0000000000..7833a672b2
--- /dev/null
+++ b/admin/app/views/spree/admin/classifications/create.turbo_stream.erb
@@ -0,0 +1,7 @@
+<%= turbo_close_modal %>
+
+<%= turbo_stream.update :product_count, @taxon.products.count %>
+
+<%= turbo_stream.replace :classifications do %>
+ <%= render template: 'spree/admin/classifications/index' %>
+<% end %>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/classifications/destroy.turbo_stream.erb b/admin/app/views/spree/admin/classifications/destroy.turbo_stream.erb
new file mode 100644
index 0000000000..9cb2d21a5a
--- /dev/null
+++ b/admin/app/views/spree/admin/classifications/destroy.turbo_stream.erb
@@ -0,0 +1,2 @@
+<%= turbo_stream.update :product_count, @taxon.products.count %>
+<%= turbo_stream.remove spree_dom_id(@classification) %>
diff --git a/admin/app/views/spree/admin/classifications/index.html.erb b/admin/app/views/spree/admin/classifications/index.html.erb
new file mode 100644
index 0000000000..1f6b1275fb
--- /dev/null
+++ b/admin/app/views/spree/admin/classifications/index.html.erb
@@ -0,0 +1,20 @@
+<%= turbo_frame_tag :classifications do %>
+ <% if @classifications.any? %>
+ <div data-controller="sortable"
+ data-sortable-handle-value=".move-handle"
+ data-sortable-resource-name-value="classification"
+ data-sortable-response-kind-value="turbo-stream">
+ <%= render partial: 'spree/admin/classifications/classification',
+ collection: @sorted_classifications,
+ cached: can?(:delete, Spree::Classification) ? spree_base_cache_scope : false %>
+ </div>
+ <% else %>
+ <p class="text-center text-muted px-5 pt-5 pb-2">
+ <%= Spree.t(:no_products_added) %>
+ </p>
+ <p class="text-center" data-toggle="modal" data-target="#modal">
+ <% if can?(:create, Spree::Classification) && @taxon.manual? %>
+ <%= link_to_with_icon 'plus', Spree.t(:add_products), spree.new_admin_taxon_classification_path(@taxon.id), class: 'btn btn-link', data: { turbo_frame: 'dialog_modal' } %>
+ <% end %>
+ <% end %>
+<% end %>
diff --git a/admin/app/views/spree/admin/classifications/new.html.erb b/admin/app/views/spree/admin/classifications/new.html.erb
new file mode 100644
index 0000000000..a680aff0b8
--- /dev/null
+++ b/admin/app/views/spree/admin/classifications/new.html.erb
@@ -0,0 +1 @@
+<%= render 'spree/admin/shared/multi_product_picker', parent: @parent, url: spree.admin_taxon_classifications_path(@parent.id) %>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/shared/_multi_product_picker.html.erb b/admin/app/views/spree/admin/shared/_multi_product_picker.html.erb
new file mode 100644
index 0000000000..ae10904b66
--- /dev/null
+++ b/admin/app/views/spree/admin/shared/_multi_product_picker.html.erb
@@ -0,0 +1,26 @@
+<%= turbo_frame_tag :dialog_modal do %>
+ <div class="modal-content" data-controller="search-picker">
+ <div class="modal-header">
+ <h5 class="modal-title" id="exampleModalLabel"><%= Spree.t(:add_products) %></h5>
+ <button type="button" class="btn-close" data-dismiss="modal" aria-label="<%= Spree.t(:close) %>" />
+ </div>
+ <div class="modal-body border-bottom pb-3">
+ <%= form_with url: spree.search_admin_products_path, method: :post, data: { controller: 'auto-submit'} do |form| %>
+ <%= form.hidden_field :omit_ids, value: parent.product_ids.join(',') %>
+ <%= form.search_field :q, value: params[:q], class: 'form-control', data: { search_picker_target: :search, action: 'auto-submit#submit' }, autofocus: true %>
+ <% end %>
+ </div>
+ <%= form_with url: url do |form| %>
+ <div class="modal-body p-0" style="min-height: 200px">
+ <div id="products_search_results" class="overflow-y-auto overflow-x-hidden" style="max-height: 300px">
+ <p class="text-muted text-center pb-3 pt-5">
+ <%= Spree.t(:start_typing_to_search_for_products) %>
+ </p>
+ </div>
+ </div>
+ <div class="modal-footer border-top">
+ <%= turbo_save_button_tag Spree.t(:add_selected_products), class: 'btn btn-primary', disabled: true, data: { search_picker_target: :submit } %>
+ </div>
+ <% end %>
+ </div>
+<% end %>
diff --git a/admin/app/views/spree/admin/shared/_no_image.html.erb b/admin/app/views/spree/admin/shared/_no_image.html.erb
new file mode 100644
index 0000000000..a614fe73a4
--- /dev/null
+++ b/admin/app/views/spree/admin/shared/_no_image.html.erb
@@ -0,0 +1,6 @@
+<% width ||= 50 %>
+<% height ||= 50 %>
+
+<div class="rounded bg-light text-muted d-flex align-items-center justify-content-center" style="width: <%= width %>px; height: <%= height %>px">
+ <%= icon('camera-off', class: 'text-muted opacity-50') %>
+</div>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/shared/_product_image.html.erb b/admin/app/views/spree/admin/shared/_product_image.html.erb
new file mode 100644
index 0000000000..908f644364
--- /dev/null
+++ b/admin/app/views/spree/admin/shared/_product_image.html.erb
@@ -0,0 +1,21 @@
+<% width ||= 50 %>
+<% height ||= 50 %>
+<% image ||= object.default_image %>
+
+<% if image.present? && image.attached? && image.variable? %>
+ <% alt ||= image.alt || object.name %>
+ <div class="admin-product-image-container">
+ <%= image_tag(
+ main_app.cdn_image_url(
+ image.variant(
+ spree_image_variant_options(resize_to_fill: [width * 2, height * 2])
+ )
+ ),
+ width: width,
+ height: height,
+ alt: alt
+ ) %>
+ </div>
+<% else %>
+ <%= render 'spree/admin/shared/no_image', width: width, height: height %>
+<% end %>
diff --git a/admin/app/views/spree/admin/shared/_seo.html.erb b/admin/app/views/spree/admin/shared/_seo.html.erb
new file mode 100644
index 0000000000..78ad2bb036
--- /dev/null
+++ b/admin/app/views/spree/admin/shared/_seo.html.erb
@@ -0,0 +1,39 @@
+<div class="card mb-4 seo-form">
+ <div class="card-header d-flex justify-content-between">
+ <h5 class="card-title"><%= Spree.t(:search_engine_listing) %></h5>
+ <button class="btn btn-light btn-sm ml-auto" data-action="click->seo-form#toggleInputs" type="button">Edit</button>
+ </div>
+ <div class="card-body d-flex flex-column">
+ <div class="seo__preview <% if title.blank? && description.blank? %>d-none<% else %>d-flex flex-column<% end %>" data-seo-form-target="preview">
+ <div class="seo__preview__title h4 font-weight-normal mb-0 text-break" data-seo-form-target="titlePreview">
+ <%= meta_title.presence || title %>
+ </div>
+ <div class="seo__preview__link">
+ <%= current_store.formatted_url %>/<%= slug_path %>/<span data-seo-form-target="slugPreview"><%= slug %></span>
+ </div>
+ <div class="seo__preview__description text-break" data-seo-form-target="descriptionPreview"></div>
+ </div>
+ <div class="seo__add-info <% if title.present? || description.present? %>d-none<% end %>" data-seo-form-target="placeholder">
+ <%= placeholder %>
+ </div>
+ <div class="seo__inputs d-none" data-seo-form-target="inputsContainer">
+ <div class="form-group">
+ <%= f.label :meta_title, Spree.t(:meta_title) %>
+ <%= f.text_field :meta_title, class: 'form-control', data: { seo_form_target: 'titleInput', action: 'input->seo-form#updatePreviews' } %>
+ </div>
+ <div class="form-group">
+ <%= f.label :meta_description, Spree.t(:meta_description) %>
+ <%= f.text_area :meta_description, class: 'form-control', rows: 4, data: { seo_form_target: 'descriptionInput', action: 'input->seo-form#updatePreviews' } %>
+ </div>
+ <div class="form-group">
+ <% if defined?(slug_attribute_name) %>
+ <%= label_tag slug_attribute_name, Spree.t(:slug) %>
+ <%= text_field_tag slug_attribute_name, slug, class: 'form-control', data: { seo_form_target: 'slugInput', action: 'input->seo-form#updatePreviews', slug_form_target: 'url' } %>
+ <% else %>
+ <%= f.label :slug, Spree.t(:slug) %>
+ <%= f.text_field :slug, class: 'form-control', data: { seo_form_target: 'slugInput', action: 'input->seo-form#updatePreviews', slug_form_target: 'url' } %>
+ <% end %>
+ </div>
+ </div>
+ </div>
+</div>
diff --git a/admin/app/views/spree/admin/taxonomies/_taxonomy.html.erb b/admin/app/views/spree/admin/taxonomies/_taxonomy.html.erb
new file mode 100644
index 0000000000..764d617a14
--- /dev/null
+++ b/admin/app/views/spree/admin/taxonomies/_taxonomy.html.erb
@@ -0,0 +1,16 @@
+<tr id="<%= spree_dom_id taxonomy %>" data-controller="row-link" data-sortable-update-url="<%= spree.admin_taxonomy_path(taxonomy) %>">
+ <td class="w-5 move-handle text-center">
+ <%= icon('grip-vertical', class: 'rounded hover-gray p-2') %>
+ </td>
+ <td class="w-50 cursor-pointer py-0" data-action="click->row-link#openLink">
+ <%= link_to taxonomy.name, spree.admin_taxonomy_path(taxonomy), class: 'text-decoration-none d-flex align-items-center font-weight-bold text-dark', data: { row_link_target: :link } %>
+ </td>
+ <td class="w-30 cursor-pointer" data-action="click->row-link#openLink">
+ <!-- We don't want to include the root taxon -->
+ <%= taxonomy.taxons.count - 1 %>
+ </td>
+ <td class="w-10 actions">
+ <%= link_to_with_icon 'list-tree', Spree.t(:manage_taxons), spree.admin_taxonomy_path(taxonomy), class: 'btn btn-light btn-sm' %>
+ <%= link_to_edit(taxonomy, class: 'btn btn-light btn-sm') if can?(:edit, taxonomy) %>
+ </td>
+</tr>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/taxonomies/edit.html.erb b/admin/app/views/spree/admin/taxonomies/edit.html.erb
new file mode 100644
index 0000000000..8121a3eb62
--- /dev/null
+++ b/admin/app/views/spree/admin/taxonomies/edit.html.erb
@@ -0,0 +1,28 @@
+<% content_for :page_title do %>
+ <%= page_header_back_button spree.admin_taxonomy_path(@taxonomy) %>
+ <%= @taxonomy.name %>
+<% end %>
+
+<% content_for :page_actions_dropdown do %>
+ <%= link_to_edit_translations(@taxonomy) %>
+<% end %>
+
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @taxonomy } %>
+
+<div class="row">
+ <div class="col-lg-6">
+ <%= form_for [:admin, @taxonomy] do |f| %>
+ <div class="card mb-4">
+ <div class="card-body">
+ <%= render partial: 'spree/admin/shared/error_messages', locals: { target: @taxonomy } %>
+ <div class="form-group">
+ <%= f.label :name, raw(Spree.t(:name) + required_span_tag) %>
+ <%= f.text_field :name, class: 'form-control' %>
+ <%= f.error_message_on :name %>
+ </div>
+ </div>
+ </div>
+ <%= render 'spree/admin/shared/edit_resource_links', f: f %>
+ <% end %>
+ </div>
+</div>
diff --git a/admin/app/views/spree/admin/taxonomies/index.html.erb b/admin/app/views/spree/admin/taxonomies/index.html.erb
new file mode 100644
index 0000000000..80a4c758b9
--- /dev/null
+++ b/admin/app/views/spree/admin/taxonomies/index.html.erb
@@ -0,0 +1,53 @@
+<% content_for(:page_title) do %>
+ <%= Spree.t(:taxonomies) %>
+<% end %>
+
+<% content_for :page_actions do %>
+ <%= button_link_to Spree.t(:new_taxonomy), new_object_url, { class: "btn-primary", icon: 'plus', } %>
+<% end if can?(:create, Spree::Taxonomy) %>
+
+<% content_for :table_filter do %>
+ <div data-hook="admin_property_sidebar">
+ <%= search_form_for [:admin, @search] do |f| %>
+
+ <%- locals = {f: f} %>
+
+ <div class="row">
+ <div class="col-12 col-lg-6">
+ <div data-hook="admin_property_index_search" class="form-group">
+ <%= f.label :name, Spree.t(:name) %>
+ <%= f.text_field :name, class: "form-control js-quick-search-target js-filterable" %>
+ </div>
+ </div>
+ </div>
+
+ <div class="form-actions">
+ <%= button Spree.t(:search), 'search' %>
+ </div>
+
+ <% end %>
+ </div>
+<% end if Spree::Taxonomy.any? %>
+
+<% if @taxonomies.present? %>
+ <div class="table-responsive">
+ <table class="table" id='listing_taxonomies'>
+ <thead class="text-muted">
+ <tr data-hook="listing_taxonomies_header">
+ <th class="no-border handel-head"></th>
+ <th><%= Spree.t(:name) %></th>
+ <th><%= Spree.t(:taxons) %></th>
+ <th class="actions"></th>
+ </tr>
+ </thead>
+ <tbody data-controller="sortable" data-sortable-handle-value=".move-handle" data-sortable-resource-name-value="taxonomy">
+ <%= render partial: 'spree/admin/taxonomies/taxonomy', collection: @collection, cached: spree_base_cache_scope %>
+ </tbody>
+ </table>
+ </div>
+<% else %>
+ <div class="text-center w-100 text-muted m-5">
+ <%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::Taxonomy)) %>
+ <%= link_to_with_icon 'plus', Spree.t(:add_one), new_object_url, class: 'btn btn-link' if can?(:create, Spree::Taxonomy) %>
+ </div>
+<% end %>
diff --git a/admin/app/views/spree/admin/taxonomies/new.html.erb b/admin/app/views/spree/admin/taxonomies/new.html.erb
new file mode 100644
index 0000000000..e509df5741
--- /dev/null
+++ b/admin/app/views/spree/admin/taxonomies/new.html.erb
@@ -0,0 +1,23 @@
+<% content_for :page_title do %>
+ <%= page_header_back_button spree.admin_taxonomies_path %>
+ <%= Spree.t(:new_taxonomy) %>
+<% end %>
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @taxonomy } %>
+
+<div class="row">
+ <div class="col-lg-6">
+ <%= form_for [:admin, @taxonomy] do |f| %>
+ <div class="card mb-4">
+ <div class="card-body">
+ <%= render partial: 'spree/admin/shared/error_messages', locals: { target: @taxonomy } %>
+ <div class="form-group">
+ <%= f.label :name, raw(Spree.t(:name) + required_span_tag) %>
+ <%= f.text_field :name, class: 'form-control' %>
+ <%= f.error_message_on :name %>
+ </div>
+ </div>
+ </div>
+ <%= render 'spree/admin/shared/new_resource_links' %>
+ <% end %>
+ </div>
+</div>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/taxonomies/show.html.erb b/admin/app/views/spree/admin/taxonomies/show.html.erb
new file mode 100644
index 0000000000..265390a24d
--- /dev/null
+++ b/admin/app/views/spree/admin/taxonomies/show.html.erb
@@ -0,0 +1,31 @@
+<% content_for :page_title do %>
+ <%= page_header_back_button spree.admin_taxonomies_path %>
+ <%= @taxonomy.name %>
+<% end %>
+
+<% content_for :page_actions do %>
+ <%= button_link_to Spree.t(:new_taxon), spree.new_admin_taxonomy_taxon_path(@taxonomy), icon: 'plus', class: 'no-wrap btn btn-primary align-self-center' %>
+<% end %>
+
+<% content_for :page_actions_dropdown do %>
+ <%= link_to_edit(@taxonomy.root, url: spree.edit_admin_taxonomy_taxon_path(@taxonomy, @taxonomy.root.id), class: 'text-left dropdown-item') %>
+<% end if can?(:edit, @taxonomy) %>
+
+<div id="sortableTreeArea">
+ <% if @taxonomy.root.present? %>
+ <div data-controller="sortable-tree" data-sortable-tree-handle-value=".move-handle">
+ <div data-sortable-tree-parent-id-value="<%= @taxonomy.root.id %>">
+ <% if @taxonomy.root.children.present? %>
+ <% @taxonomy.root.children.includes({ image_attachment: :blob, square_image_attachment: :blob }).each do |root_children| %>
+ <%= build_sortable_tree(@taxonomy, root_children) %>
+ <% end %>
+ <% else %>
+ <div class="text-muted p-5 d-flex align-items-center w-100 justify-content-center">
+ <%= Spree.t(:no_resource_found, resource: plural_resource_name(Spree::Taxon)) %>
+ <%= link_to_with_icon 'plus', Spree.t(:add_one), spree.new_admin_taxonomy_taxon_path(@taxonomy), class: 'btn btn-link' if can?(:create, Spree::Taxon) %>
+ </div>
+ <% end %>
+ </div>
+ </div>
+ <% end %>
+</div>
diff --git a/admin/app/views/spree/admin/taxons/_form.html.erb b/admin/app/views/spree/admin/taxons/_form.html.erb
new file mode 100644
index 0000000000..5917731e33
--- /dev/null
+++ b/admin/app/views/spree/admin/taxons/_form.html.erb
@@ -0,0 +1,149 @@
+<div class="row" data-controller="slug-form seo-form">
+ <div class="col-lg-8">
+ <div class="card mb-4">
+ <div class="card-body pb-0 pt-3">
+ <div class="form-group">
+ <%= f.label :name, raw(Spree.t(:name) + required_span_tag) %>
+ <%= f.text_field :name,
+ class: 'form-control',
+ required: true,
+ autofocus: @taxon.new_record?,
+ data: {
+ seo_form_target: 'sourceTitleInput',
+ slug_form_target: 'name',
+ action: 'slug-form#updateUrlFromName'
+ } %>
+ <%= f.error_message_on :name %>
+ </div>
+ <div class="form-group">
+ <%= f.label :description, Spree.t(:description) %>
+ <div class="trix-container">
+ <%= f.rich_text_area :description, data: { seo_form_target: 'sourceDescriptionInput' } %>
+ </div>
+ </div>
+ </div>
+ </div>
+
+ <% if @taxon.new_record? %>
+ <div class="card mb-4" data-controller="reveal" data-reveal-hidden-class="d-none">
+ <div class="card-header">
+ <h5 class="card-title"><%= Spree.t('admin.taxon_type') %></h5>
+ </div>
+ <div class="card-body">
+ <div class="custom-control custom-radio mb-2">
+ <%= f.radio_button :automatic, false, id: 'customRadio1', class: 'custom-control-input', checked: true, data: { action: 'reveal#hide' } %>
+ <label class="custom-control-label" for="customRadio1"><%= Spree.t('admin.taxon_types.manual') %></label>
+ <br />
+ <span class="text-muted"><%= Spree.t('admin.taxon_types.manual_info') %></span>
+ </div>
+ <div class="custom-control custom-radio">
+ <%= f.radio_button :automatic, true, id: 'customRadio2', class: 'custom-control-input', data: { action: 'reveal#show' } %>
+ <label class="custom-control-label" for="customRadio2"><%= Spree.t('admin.taxon_types.automatic') %></label>
+ <br />
+ <span class="text-muted"><%= Spree.t('admin.taxon_types.automatic_info') %></span>
+ </div>
+ </div>
+ <div class="card-body border-top pt-4 d-none" data-reveal-target="item">
+ <h6><%= Spree.t(:conditions) %></h6>
+ <%= render 'spree/admin/taxons/rules_form', f: f %>
+ </div>
+ </div>
+ <% else %>
+ <% if @taxon.automatic? %>
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title"><%= Spree.t(:conditions) %></h5>
+ </div>
+ <div class="card-body">
+ <div class="alert alert-info mb-3"><%= Spree.t('admin.taxon_types.automatic_info') %></div>
+ <%= render 'spree/admin/taxons/rules_form', f: f %>
+ </div>
+ </div>
+ <% end %>
+
+ <div class="card mb-4">
+ <div class="card-header d-flex align-items-center justify-content-between">
+ <h5 class="card-title">
+ <%= Spree.t(:products) %>
+ <span class="badge badge-info ml-2" id="product_count">
+ <%= @taxon.products.not_archived.count %>
+ </span>
+ </h5>
+
+ <% if @taxon.manual? %>
+ <span data-toggle="modal" data-target="#modal">
+ <%= link_to_with_icon 'plus', Spree.t(:add_products), spree.new_admin_taxon_classification_path(@taxon.id), class: 'btn btn-light btn-sm', data: { turbo_frame: 'dialog_modal' } %>
+ </span>
+ <% end %>
+ </div>
+
+ <div class="card-body border-bottom">
+ <div class="form-inline">
+ <%= f.label :sort_order %>
+ <%= f.select :sort_order, taxon_sort_options_for_select, {}, { class: 'custom-select custom-select-sm ml-lg-3', data: { action: 'change->auto-submit#submitForm' } } %>
+ </div>
+ </div>
+
+ <div class="card-body p-0" style="min-height: 300px">
+ <div class="d-block overflow-y-auto overflow-x-hidden" style="max-height: 300px">
+ <%= turbo_frame_tag :classifications, src: spree.admin_taxon_classifications_path(@taxon.id), loading: :lazy do %>
+ <%= render 'shared/spinner' %>
+ <% end %>
+ </div>
+ </div>
+ </div>
+ <% end %>
+ </div>
+
+ <div class="col-lg-4">
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title"><%= Spree.t(:images) %></h5>
+ </div>
+ <div class="card-body">
+ <%= f.label :image, Spree.t(:image) %>
+ <%= render 'active_storage/upload_form', form: f, field_name: :image, width: 1920, height: 1080 %>
+ <%= f.error_message_on :image %>
+ </div>
+
+ <div class="card-body">
+ <%= f.label :square_image, Spree.t(:square_image) %>
+ <p class="text-muted form-text">
+ Used for the featured image on the homepage.
+ </p>
+ <%= render 'active_storage/upload_form', form: f, field_name: :square_image, width: 400, height: 400, crop: true %>
+ <%= f.error_message_on :square_image %>
+ </div>
+ </div>
+
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title">
+ <%= Spree.t(:navigation) %>
+ </h5>
+ </div>
+ <div class="card-body pb-0">
+ <% if @taxon.new_record? %>
+ <% disabled = false %>
+ <% else %>
+ <% disabled = @taxon.root? %>
+ <% end %>
+ <div class="form-group">
+ <%= f.label :parent_id, Spree.t('admin.navigation.nested_under') %>
+ <%= f.select :parent_id, nested_set_options(@taxonomy.taxons.includes(:parent), @taxon) {|i| i.pretty_name }, { include_blank: false }, { data: { controller: 'autocomplete-select' }, disabled: disabled } %>
+ <%= f.error_message_on :parent_id %>
+ </div>
+ </div>
+ </div>
+
+ <%= render 'spree/admin/shared/seo',
+ f: f,
+ title: @taxon.name,
+ meta_title: @taxon.meta_title,
+ description: @taxon.description,
+ slug_attribute_name: :permalink_part,
+ slug: @permalink_part,
+ slug_path: ['t', @parent_permalink.presence].compact.join('/'),
+ placeholder: 'Add a title and description to see how this taxon might appear in a search engine listing' %>
+ </div>
+</div>
diff --git a/admin/app/views/spree/admin/taxons/_rule_form.html.erb b/admin/app/views/spree/admin/taxons/_rule_form.html.erb
new file mode 100644
index 0000000000..a74a2f5074
--- /dev/null
+++ b/admin/app/views/spree/admin/taxons/_rule_form.html.erb
@@ -0,0 +1,24 @@
+<div class="row nested-form-wrapper border-bottom mb-3 pr-2" data-controller="rule-form" data-new-record="<%= rf.object.new_record? %>">
+ <% @taxon_rules.each do |taxon_rule| %>
+ <template id="rule-form-template-<%= taxon_rule.to_s.demodulize %>" data-rule-form-target="template">
+ <%= render "spree/admin/taxons/rule_forms/#{taxon_rule.to_s.demodulize.underscore}", rule_form: rf %>
+ </template>
+ <% end %>
+
+ <div class="col-lg-4 mb-3">
+ <%= rf.select :type, @rule_types, {}, { class: 'custom-select', data: { action: 'rule-form#replace' } } %>
+ </div>
+ <div class="col-lg-3 mb-3">
+ <%= rf.select :match_policy, @rule_match_policies, {}, { class: 'custom-select' } %>
+ </div>
+ <div class="col-lg-4 mb-3" data-rule-form-target="value">
+ <%= render "spree/admin/taxons/rule_forms/#{rf.object.type.demodulize.underscore}", rule_form: rf %>
+ </div>
+ <div class="col-lg-1 mb-3 d-flex align-items-center justify-content-center">
+ <button type="button" data-action="nested-form#remove" class="btn btn-sm btn-danger px-2 with-tip" title="Remove condition">
+ <%= icon('x', class: 'mr-0') %>
+ <span class="d-lg-none"><%= Spree.t(:remove_condition) %></span>
+ </button>
+ <%= rf.hidden_field :_destroy if rf.object.persisted? %>
+ </div>
+</div>
diff --git a/admin/app/views/spree/admin/taxons/_rules_form.html.erb b/admin/app/views/spree/admin/taxons/_rules_form.html.erb
new file mode 100644
index 0000000000..a2dcedfe55
--- /dev/null
+++ b/admin/app/views/spree/admin/taxons/_rules_form.html.erb
@@ -0,0 +1,36 @@
+<div id="rules-form">
+ <div class="form-group mb-1">
+ <span class="d-block d-lg-inline mb-3"><%= Spree.t('admin.taxon_rules.products_must_match') %></span>
+
+ <div class="custom-control custom-radio custom-control-inline ml-lg-3 mb-3">
+ <%= f.radio_button :rules_match_policy, :all, class: 'custom-control-input' %>
+ <%= f.label :rules_match_policy, Spree.t('admin.taxon_rules.all_conditions'), class: 'custom-control-label', for: :taxon_rules_match_policy_all %>
+ </div>
+ <div class="custom-control custom-radio custom-control-inline mb-3">
+ <%= f.radio_button :rules_match_policy, :any, class: 'custom-control-input' %>
+ <%= f.label :rules_match_policy, Spree.t('admin.taxon_rules.any_condition'), class: 'custom-control-label', for: :taxon_rules_match_policy_any %>
+ </div>
+ </div>
+
+ <div data-controller="nested-form">
+ <template data-nested-form-target="template">
+ <%= f.fields_for :taxon_rules, @taxon.taxon_rules.new(type: @taxon_rules.first.to_s), child_index: 'NEW_RECORD' do |rf| %>
+ <%= render 'spree/admin/taxons/rule_form', rf: rf %>
+ <% end %>
+ </template>
+
+ <%= f.fields_for :taxon_rules, @taxon.taxon_rules do |rf| %>
+ <%= render 'spree/admin/taxons/rule_form', rf: rf %>
+ <% end %>
+
+ <!-- Inserted elements will be injected before that target. -->
+ <div data-nested-form-target="target" class="mb-3"></div>
+
+ <div class="text-center">
+ <button type="button" class="btn btn-link" data-action="nested-form#add">
+ <%= icon('plus', class: 'mr-2') %>
+ <%= Spree.t(:add_one)%>
+ </button>
+ </div>
+ </div>
+</div>
diff --git a/admin/app/views/spree/admin/taxons/destroy.turbo_stream.erb b/admin/app/views/spree/admin/taxons/destroy.turbo_stream.erb
new file mode 100644
index 0000000000..6d42e94dba
--- /dev/null
+++ b/admin/app/views/spree/admin/taxons/destroy.turbo_stream.erb
@@ -0,0 +1 @@
+<%= turbo_stream.remove spree_dom_id(@taxon) %>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/taxons/edit.html.erb b/admin/app/views/spree/admin/taxons/edit.html.erb
new file mode 100644
index 0000000000..2483f721e6
--- /dev/null
+++ b/admin/app/views/spree/admin/taxons/edit.html.erb
@@ -0,0 +1,19 @@
+<% content_for :page_title do %>
+ <%= page_header_back_button spree.admin_taxonomy_path(@taxon.taxonomy) %>
+ <%= @taxon.name %>
+<% end %>
+
+<% content_for :page_actions_dropdown do %>
+ <%= link_to_edit_translations(@taxon) %>
+ <%= external_page_preview_link(@taxon, name: Spree.t(:taxonomy)) %>
+<% end %>
+
+<%# Because otherwise the form would attempt to use to_param of @taxon %>
+<% form_url = admin_taxonomy_taxon_path(@taxonomy.id, @taxon.id) %>
+
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @taxon } %>
+
+<%= form_for @taxon, as: :taxon, method: :put, url: form_url, html: { multipart: true }, data: { auto_submit_target: 'form', controller: 'auto-submit' } do |f| %>
+ <%= render 'form', f: f %>
+ <%= render 'spree/admin/shared/edit_resource_links', f: f %>
+<% end %>
diff --git a/admin/app/views/spree/admin/taxons/new.html.erb b/admin/app/views/spree/admin/taxons/new.html.erb
new file mode 100644
index 0000000000..a9df902dc1
--- /dev/null
+++ b/admin/app/views/spree/admin/taxons/new.html.erb
@@ -0,0 +1,12 @@
+<% content_for :page_title do %>
+ <%= page_header_back_button spree.admin_taxonomy_path(@taxonomy) %>
+ <%= Spree.t(:new_taxon) %>
+<% end %>
+
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @taxon } %>
+
+<%= form_for @taxon, as: :taxon, url: spree.admin_taxonomy_taxons_path do |f| %>
+ <%= render 'form', f: f %>
+
+ <%= render 'spree/admin/shared/new_resource_links' %>
+<% end %>
diff --git a/admin/app/views/spree/admin/taxons/rule_forms/_available_on.html.erb b/admin/app/views/spree/admin/taxons/rule_forms/_available_on.html.erb
new file mode 100644
index 0000000000..286822d3df
--- /dev/null
+++ b/admin/app/views/spree/admin/taxons/rule_forms/_available_on.html.erb
@@ -0,0 +1,4 @@
+<div class="d-flex align-items-center justify-content-center h-100">
+ <%= rule_form.number_field :value, min: 1, class: 'form-control mr-3' %>
+ <span class="form-text text-nowrap"><%= Spree.t(:days_ago) %></span>
+</div>
diff --git a/admin/app/views/spree/admin/taxons/rule_forms/_sale.html.erb b/admin/app/views/spree/admin/taxons/rule_forms/_sale.html.erb
new file mode 100644
index 0000000000..227dab4ceb
--- /dev/null
+++ b/admin/app/views/spree/admin/taxons/rule_forms/_sale.html.erb
@@ -0,0 +1,4 @@
+<div class="d-flex align-items-center justify-content-center h-100">
+ <span class="badge badge-success"><%= Spree.t(:true_label) %></span>
+ <%= rule_form.hidden_field :value, value: true %>
+</div>
diff --git a/admin/app/views/spree/admin/taxons/rule_forms/_tag.html.erb b/admin/app/views/spree/admin/taxons/rule_forms/_tag.html.erb
new file mode 100644
index 0000000000..2171e64b08
--- /dev/null
+++ b/admin/app/views/spree/admin/taxons/rule_forms/_tag.html.erb
@@ -0,0 +1 @@
+<%= rule_form.select :value, tags_scope.distinct.pluck(:name, :name), { include_blank: true }, data: { controller: 'autocomplete-select' } %>
diff --git a/admin/config/locales/en.yml b/admin/config/locales/en.yml
index c7a69ae5ba..67655888dc 100644
--- a/admin/config/locales/en.yml
+++ b/admin/config/locales/en.yml
@@ -87,6 +87,24 @@ en:
submenu: Submenu
add_new_taxon: Add a new Taxon
no_taxons: '<b>%{taxonomy}</b> has no Taxons. Click the <b>Add a new Taxon</b> button, to begin adding Taxons.'
+ taxon_type: Taxon type
+ taxon_types:
+ manual: Manual
+ manual_info: Curate products manually. You can add or remove them in bulk.
+ automatic: Automatic
+ automatic_info: Automatically match products to this taxon based on the conditions you set.
+ taxon_rules:
+ products_must_match: "Products must match:"
+ all_conditions: all conditions
+ any_condition: any condition
+ tag: Product tag
+ available_on: Available on
+ on_sale: On sale
+ match_policies:
+ is_equal_to: is equal to
+ is_not_equal_to: is not equal to
+ contains: contains
+ does_not_contain: does not contain
order:
changing_store_warning: If you change the store this order belongs to, the order will be reset.
once_an_order_is_complete_you_can_not_change_store: 'Sorry, once an order is complete, you can no longer change the store that the order belongs to.'
diff --git a/admin/config/routes.rb b/admin/config/routes.rb
index 3d3fc786eb..de700818fd 100644
--- a/admin/config/routes.rb
+++ b/admin/config/routes.rb
@@ -43,6 +43,24 @@ Spree::Core::Engine.add_routes do
resources :oauth_applications
resources :webhooks_subscribers
+ # products
+ resources :products
+
+ # taxonomies and taxons
+ resources :taxonomies do
+ resources :taxons do
+ member do
+ put :reposition
+ end
+ end
+ end
+
+ resources :taxons, except: [:show] do |_taxon|
+ resources :classifications, only: %i[index new create update destroy]
+ end
+
+ get '/taxons/select_options' => 'taxons#select_options', as: :taxons_select_options, defaults: { format: :json }
+
# errors
get '/forbidden', to: 'errors#forbidden', as: :forbidden
end
diff --git a/admin/spec/controllers/spree/admin/classifications_controller_spec.rb b/admin/spec/controllers/spree/admin/classifications_controller_spec.rb
new file mode 100644
index 0000000000..4bfde03c99
--- /dev/null
+++ b/admin/spec/controllers/spree/admin/classifications_controller_spec.rb
@@ -0,0 +1,60 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::ClassificationsController, type: :controller do
+ let(:store) { Spree::Store.default }
+
+ stub_authorization!
+
+ render_views
+
+ let(:taxon) { create(:taxon) }
+ let(:product1) { create(:product) }
+ let(:product2) { create(:product) }
+
+ describe 'GET #index' do
+ let!(:classifications) do
+ [create(:classification, taxon: taxon, product: product1),
+ create(:classification, taxon: taxon, product: product2)]
+ end
+
+ subject { get :index, params: { taxon_id: taxon.id } }
+
+ it 'returns a successful response' do
+ subject
+ expect(response).to have_http_status(:success)
+ end
+
+ it 'assigns @classifications' do
+ subject
+ expect(assigns(:classifications)).to contain_exactly(*classifications)
+ end
+ end
+
+ describe 'POST #create' do
+ subject { post :create, params: { taxon_id: taxon.id, product_ids: [product1.id, product2.id], format: :turbo_stream } }
+
+ it 'creates classifications' do
+ expect { subject }.to change(Spree::Classification, :count).by(2)
+ end
+
+ it 'returns a successful response' do
+ subject
+ expect(response).to have_http_status(:success)
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ let!(:classification) { create(:classification, taxon: taxon, product: product1) }
+
+ subject { delete :destroy, params: { taxon_id: taxon.id, id: classification.id, format: :turbo_stream } }
+
+ it 'destroys the classification' do
+ expect { subject }.to change(Spree::Classification, :count).by(-1)
+ end
+
+ it 'returns a successful response' do
+ subject
+ expect(response).to have_http_status(:success)
+ end
+ end
+end
diff --git a/admin/spec/controllers/spree/admin/taxons_controller_spec.rb b/admin/spec/controllers/spree/admin/taxons_controller_spec.rb
new file mode 100644
index 0000000000..af9e595e54
--- /dev/null
+++ b/admin/spec/controllers/spree/admin/taxons_controller_spec.rb
@@ -0,0 +1,47 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::TaxonsController, type: :controller do
+ stub_authorization!
+
+ render_views
+
+ let(:store) { Spree::Store.default }
+ let(:taxonomy) { create(:taxonomy, store: store) }
+
+ describe 'GET #new' do
+ let(:taxon) { create(:taxon, taxonomy: taxonomy) }
+
+ it 'returns a successful response' do
+ get :new, params: { taxonomy_id: taxonomy.id, taxon: { parent_id: taxon.id } }
+ expect(response).to be_successful
+ end
+
+ it 'assigns the parent taxon' do
+ get :new, params: { taxonomy_id: taxonomy.id, taxon: { parent_id: taxon.id } }
+ expect(assigns(:taxon).parent).to eq(taxon)
+ end
+ end
+
+ describe 'PUT #reposition' do
+ let(:taxon) { create(:taxon, taxonomy: taxonomy) }
+ let(:new_parent) { create(:taxon, taxonomy: taxonomy) }
+
+ it 'returns a successful response' do
+ put :reposition, params: { taxonomy_id: taxonomy.id, id: taxon.id, taxon: { new_parent_id: new_parent.id, new_position_idx: 0 } }
+ expect(response).to be_successful
+ end
+
+ it 'repositions the taxon' do
+ put :reposition, params: { taxonomy_id: taxonomy.id, id: taxon.id, taxon: { new_parent_id: new_parent.id, new_position_idx: 0 } }
+ expect(taxon.reload.parent).to eq(new_parent)
+ end
+ end
+
+ describe 'DELETE #destroy' do
+ let!(:taxon) { create(:taxon, taxonomy: taxonomy) }
+
+ it 'removes the taxon from the database' do
+ expect { delete :destroy, params: { taxonomy_id: taxonomy.id, id: taxon.id }, format: :turbo_stream }.to change(Spree::Taxon, :count).by(-1)
+ end
+ end
+end
diff --git a/core/app/sorters/spree/classifications/sort.rb b/core/app/sorters/spree/classifications/sort.rb
new file mode 100644
index 0000000000..14b7bbce76
--- /dev/null
+++ b/core/app/sorters/spree/classifications/sort.rb
@@ -0,0 +1,70 @@
+module Spree
+ module Classifications
+ class Sort < ::Spree::BaseSorter
+ def initialize(scope, current_currency, params = {}, allowed_sort_attributes = [])
+ super(scope.reorder(''), params, allowed_sort_attributes)
+
+ @currency = params[:currency] || current_currency
+ end
+
+ def call
+ classifications = by_manual(scope)
+ classifications = by_best_selling(classifications)
+ classifications = by_param_attributes(classifications)
+ by_price(classifications)
+ end
+
+ private
+
+ attr_reader :sort, :scope, :currency, :allowed_sort_attributes
+
+ def by_manual(scope)
+ return scope unless sort_by?('manual')
+
+ scope.order(position: :asc)
+ end
+
+ def by_best_selling(scope)
+ return scope unless (value = sort_by?('best_selling'))
+
+ scope.
+ select("#{Spree::Classification.table_name}.*, #{Spree::Product.table_name}.orders_count, #{Spree::Product.table_name}.orders_total").
+ order("#{Spree::Product.table_name}.orders_count #{value[1]}, #{Spree::Product.table_name}.orders_total #{value[1]}")
+ end
+
+ def by_param_attributes(scope)
+ return scope if sort.empty?
+
+ sort.each do |value, order|
+ next if value.blank? || Spree::Product.column_names.exclude?(value)
+
+ table_name = if Spree.use_translations?
+ translatable_fields = Spree::Product::TRANSLATABLE_FIELDS
+ value.to_sym.in?(translatable_fields) ? Spree::Product.translation_table_alias : Spree::Product.table_name
+ else
+ Spree::Product.table_name
+ end
+
+ scope = scope.order("#{table_name}.#{value}": order)
+ end
+
+ scope
+ end
+
+ def by_price(scope)
+ return scope unless (value = sort_by?('price'))
+
+ scope.joins(product: { variants_including_master: :prices }).
+ select("#{Spree::Classification.table_name}.*, min(#{Spree::Price.table_name}.amount)").
+ distinct.
+ where(spree_prices: { currency: currency }).
+ order("min(#{Spree::Price.table_name}.amount) #{value[1]}").
+ group("#{Spree::Classification.table_name}.id")
+ end
+
+ def sort_by?(field)
+ sort.detect { |s| s[0] == field }
+ end
+ end
+ end
+end
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 83d777d727..825b59b4cf 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -1711,6 +1711,7 @@ en:
taxonomy_tree_error: The requested change has not been accepted and the tree has been returned to its previous state, please try again.
taxonomy_tree_instruction: ! '* Right click (or double tap on iOS device) a child in the tree to access the menu for adding, deleting or sorting a child.'
taxons: Taxons
+ true_label: "TRUE"
automatic_taxon_names:
on_sale: On sale
new_arrivals: New arrivals
diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rb
index fdac635ae7..5f3c4866f3 100644
--- a/core/lib/spree/core/engine.rb
+++ b/core/lib/spree/core/engine.rb
@@ -13,7 +13,8 @@ module Spree
:promotions,
:line_item_comparison_hooks,
:data_feed_types,
- :export_types)
+ :export_types,
+ :taxon_rules)
SpreeCalculators = Struct.new(:shipping_methods, :tax_rates, :promotion_actions_create_adjustments, :promotion_actions_create_item_adjustments)
PromoEnvironment = Struct.new(:rules, :actions)
isolate_namespace Spree
@@ -136,6 +137,12 @@ module Spree
Spree::Exports::Products,
Spree::Exports::Orders
]
+
+ Rails.application.config.spree.taxon_rules = [
+ Spree::TaxonRules::Tag,
+ Spree::TaxonRules::AvailableOn,
+ Spree::TaxonRules::Sale,
+ ]
end
initializer 'spree.promo.register.promotions.actions' do |app|