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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Allow user to sort products by name on PLP
Damian Kaczmarczyk436ddcbe7423
diff --git a/core/app/finders/spree/products/find.rb b/core/app/finders/spree/products/find.rbindex d71895e04c..9ecf2f596d 100644--- a/core/app/finders/spree/products/find.rb+++ b/core/app/finders/spree/products/find.rb@@ -198,6 +198,10 @@ module Spreeelseproductsend+ when 'name-a-z'+ products.order(name: :asc)+ when 'name-z-a'+ products.order(name: :desc)when 'newest-first'products.order(available_on: :desc)when 'price-high-to-low'diff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex f6fb07bcdb..eed7b3f331 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -1331,6 +1331,8 @@ en:price: priceprice_high_to_low: PRICE (HIGH - LOW)price_low_to_high: PRICE (LOW - HIGH)+ name_a_z: NAME (A-Z)+ name_z_a: NAME (Z-A)result: RESULTresults_for: RESULTS FORsize: SIZEdiff --git a/frontend/app/views/spree/products/_sort_desktop.html.erb b/frontend/app/views/spree/products/_sort_desktop.html.erbindex 876627ec8d..1a87ff36e1 100644--- a/frontend/app/views/spree/products/_sort_desktop.html.erb+++ b/frontend/app/views/spree/products/_sort_desktop.html.erb@@ -10,21 +10,27 @@width: 18,height: 18) %></a>-<div class="dropdown-menu text-right plp-sort-dropdown"><ul class="pt-1 plp-sort-dropdown-ul"><li class="p-3 plp-sort-dropdown-ul-li<%= '--active' if params[:sort_by].blank? %>"><%= link_to Spree.t('plp.default'), url_for(permitted_params.merge(sort_by: nil)), class: 'dropdown-item' %></li>- <li class="p-3 plp-sort-dropdown-ul-li<%= '--active' if params[:sort_by] == 'newest-first' %>">- <%= link_to Spree.t('plp.newest_first'), url_for(permitted_params.merge(sort_by: 'newest-first')), class: 'dropdown-item' %>+ <li class="p-3 plp-sort-dropdown-ul-li<%= '--active' if params[:sort_by] == 'name-a-z' %>">+ <%= link_to Spree.t('plp.name_a_z'), url_for(permitted_params.merge(sort_by: 'name-a-z')), class: 'dropdown-item' %></li>- <li class="p-3 plp-sort-dropdown-ul-li<%= '--active' if params[:sort_by] == 'price-high-to-low' %>">- <%= link_to Spree.t('plp.price_high_to_low'), url_for(permitted_params.merge(sort_by: 'price-high-to-low')), class: 'dropdown-item' %>- </li>- <li class="p-3 plp-sort-dropdown-ul-li<%= '--active' if params[:sort_by] == 'price-low-to-high' %>">- <%= link_to Spree.t('plp.price_low_to_high'), url_for(permitted_params.merge(sort_by: 'price-low-to-high')), class: 'dropdown-item' %>- </li>- </ul>- </div>+ </li>+ <li class="p-3 plp-sort-dropdown-ul-li<%= '--active' if params[:sort_by] == 'name-z-a' %>">+ <%= link_to Spree.t('plp.name_z_a'), url_for(permitted_params.merge(sort_by: 'name-z-a')), class: 'dropdown-item' %>+ </li>+ <li class="p-3 plp-sort-dropdown-ul-li<%= '--active' if params[:sort_by] == 'newest-first' %>">+ <%= link_to Spree.t('plp.newest_first'), url_for(permitted_params.merge(sort_by: 'newest-first')), class: 'dropdown-item' %>+ </li>+ <li class="p-3 plp-sort-dropdown-ul-li<%= '--active' if params[:sort_by] == 'price-high-to-low' %>">+ <%= link_to Spree.t('plp.price_high_to_low'), url_for(permitted_params.merge(sort_by: 'price-high-to-low')), class: 'dropdown-item' %>+ </li>+ <li class="p-3 plp-sort-dropdown-ul-li<%= '--active' if params[:sort_by] == 'price-low-to-high' %>">+ <%= link_to Spree.t('plp.price_low_to_high'), url_for(permitted_params.merge(sort_by: 'price-low-to-high')), class: 'dropdown-item' %>+ </li>+ </ul>+</div></div>diff --git a/frontend/app/views/spree/products/_sort_mobile.html.erb b/frontend/app/views/spree/products/_sort_mobile.html.erbindex 93ea63b0bf..7da579f2d5 100644--- a/frontend/app/views/spree/products/_sort_mobile.html.erb+++ b/frontend/app/views/spree/products/_sort_mobile.html.erb@@ -15,6 +15,22 @@<%= Spree.t('plp.default') %></a></li>+ <li class="plp-overlay-ul-li <%= 'plp-overlay-ul-li--active' if params[:sort_by] == 'name-a-z' %>">+ <a class="p-3 d-block" href="<%= url_for(permitted_params.merge(sort_by: 'name-a-z')) %>">+ <div class="plp-empty-dot">+ <div class="filled-dot"></div>+ </div>+ <%= Spree.t('plp.name_a_z') %>+ </a>+ </li>+ <li class="plp-overlay-ul-li <%= 'plp-overlay-ul-li--active' if params[:sort_by] == 'name-z-a' %>">+ <a class="p-3 d-block" href="<%= url_for(permitted_params.merge(sort_by: 'name-z-a')) %>">+ <div class="plp-empty-dot">+ <div class="filled-dot"></div>+ </div>+ <%= Spree.t('plp.name_z_a') %>+ </a>+ </li><li class="plp-overlay-ul-li <%= 'plp-overlay-ul-li--active' if params[:sort_by] == 'newest-first' %>"><a class="p-3 d-block" href="<%= url_for(permitted_params.merge(sort_by: 'newest-first')) %>"><div class="plp-empty-dot">diff --git a/frontend/spec/features/products_sorting_spec.rb b/frontend/spec/features/products_sorting_spec.rbnew file mode 100644index 0000000000..ef28a2e17b--- /dev/null+++ b/frontend/spec/features/products_sorting_spec.rb@@ -0,0 +1,64 @@+require 'spec_helper'++describe 'Products sorting', type: :feature, js: true do+ let(:store) { Spree::Store.default }++ def wait_for_turbolinks+ expect(page).to have_no_css '.turbolinks-progress-bar'+ end++ def apply_sorting(sort_by)+ dropdown_element = find('div.plp-sort')++ dropdown_element.find('a', match: :first).click unless dropdown_element[:class].include? 'show'+ find('a', text: Spree.t("plp.#{sort_by}")).click+ end++ def products_names+ all('.product-component-name').map(&:text)+ end++ before do+ visit spree.products_path+ create_list(:product, 10, stores: [store]).each do |p|+ p.price = rand(1..100)+ p.save+ end+ store.products.reload+ end++ it 'allows sorting products by name (A-Z)' do+ apply_sorting('name_a_z')+ wait_for_turbolinks++ expect(products_names).to eql(store.products.order(name: :asc).pluck(:name))+ end++ it 'allows sorting products by name (Z-A)' do+ apply_sorting('name_z_a')+ wait_for_turbolinks++ expect(products_names).to eql(store.products.order(name: :desc).pluck(:name))+ end++ it 'allows sorting products by newest first' do+ apply_sorting('newest_first')+ wait_for_turbolinks++ expect(products_names).to eql(store.products.order(available_on: :desc).pluck(:name))+ end++ it 'allows sorting products by price (high to low)' do+ apply_sorting('price_high_to_low')+ wait_for_turbolinks++ expect(products_names).to eql(store.products.sort_by(&:price).reverse.map(&:name))+ end++ it 'allows sorting products by price (low to high)' do+ apply_sorting('price_low_to_high')+ wait_for_turbolinks++ expect(products_names).to eql(store.products.sort_by(&:price).map(&:name))+ end+end