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.

Storefront: product list page cont.

Damian Legawiec420c39715fd7
diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb
index 1a71e0b200..98c6a637ba 100644
--- a/core/lib/spree/core.rb
+++ b/core/lib/spree/core.rb
@@ -1,3 +1,4 @@
+require 'ostruct'
require 'action_controller/railtie'
require 'action_view/railtie'
require 'active_job/railtie'
diff --git a/storefront/app/controllers/spree/products_controller.rb b/storefront/app/controllers/spree/products_controller.rb
index fa88763402..534a77c11a 100644
--- a/storefront/app/controllers/spree/products_controller.rb
+++ b/storefront/app/controllers/spree/products_controller.rb
@@ -60,7 +60,7 @@ module Spree
{}
end
- variant_finder = ::Storefront::VariantFinder.new(
+ variant_finder = Spree::Storefront::VariantFinder.new(
product: @product,
variant_id: params[:variant_id],
current_currency: current_currency,
@@ -68,7 +68,7 @@ module Spree
)
@selected_variant, @variant_from_options =
- Rails.cache.fetch([@product.cache_key_with_version, "variant-finder", current_currency, params[:variant_id], options_hash].compact) do
+ Rails.cache.fetch([@product.cache_key_with_version, 'variant-finder', current_currency, params[:variant_id], options_hash].compact) do
variant_finder.find
end
end
diff --git a/storefront/app/finders/spree/storefront/variant_finder.rb b/storefront/app/finders/spree/storefront/variant_finder.rb
new file mode 100644
index 0000000000..4971bf4cf6
--- /dev/null
+++ b/storefront/app/finders/spree/storefront/variant_finder.rb
@@ -0,0 +1,80 @@
+module Spree
+ module Storefront
+ class VariantFinder
+ def initialize(product:, current_currency:, variant_id: nil, options_hash: {})
+ @product = product
+ @variant_id = variant_id
+ @current_currency = current_currency
+ @options_hash = options_hash
+ end
+
+ def find
+ try_variant_from_options
+ return [@selected_variant, @variant_from_options] if @selected_variant.present?
+
+ try_any_variant
+ [@selected_variant, @variant_from_options]
+ end
+
+ private
+
+ attr_reader :current_currency, :options_hash, :variant_id
+
+ def try_variant_from_options
+ return unless options_hash.present?
+ return unless (@product.option_type_ids.map(&:to_s) - options_hash.keys).empty?
+
+ @product.variants.each do |variant|
+ options_matched = options_hash.all? do |option_type_id, name|
+ variant.option_values.any? { |ov| ov.option_type_id.to_s == option_type_id && ov.name.downcase.strip == name.downcase.strip }
+ end
+
+ if options_matched && variant.purchasable? && variant.price_in(current_currency).amount.present?
+ @selected_variant = variant
+ @variant_from_options = variant
+ break
+ elsif options_matched
+ @variant_from_options = variant
+ break
+ end
+ end
+ end
+
+ def try_any_variant
+ if @product.has_variants?
+ if options_hash.present? # if selected combination is not purchasable nor active
+ @selected_variant = nil
+ elsif variant_id.present?
+ variant = @product.variants.find(variant_id)
+ @variant_from_options = variant
+ @selected_variant = variant
+ elsif product_option_types.size == 1 && product_option_types.first.color?
+ @selected_variant = @variant_from_options = @product.variants.find { |v| v.purchasable? && v.price_in(current_currency).amount.present? }
+ elsif @product.variants.size == 1 || product_has_only_one_secondary_option_type_value?
+ @variant_from_options = @product.variants.first
+ @selected_variant = @variant_from_options
+ end
+ elsif @product.master.purchasable? && @product.master.price_in(current_currency).amount.present?
+ @selected_variant = @variant_from_options = @product.master
+ else
+ @selected_variant = nil
+ @variant_from_options = @product.master
+ end
+ end
+
+ def product_has_only_one_secondary_option_type_value?
+ return false unless product_option_types.size == 2
+ return false unless product_option_types.first.color?
+
+ @product.
+ option_values.
+ select { |ov| ov.option_type_id == product_option_types.last.id }.
+ uniq(&:id).size == 1
+ end
+
+ def product_option_types
+ @product_option_types ||= @product.option_types.to_a
+ end
+ end
+ end
+end
diff --git a/storefront/app/views/spree/shared/_load_more_products.turbo_stream.erb b/storefront/app/views/spree/shared/_load_more_products.turbo_stream.erb
new file mode 100644
index 0000000000..f0b9839609
--- /dev/null
+++ b/storefront/app/views/spree/shared/_load_more_products.turbo_stream.erb
@@ -0,0 +1,7 @@
+<%= turbo_stream.replace "next_page" do %>
+ <%= render 'spree/products/show_more_button' %>
+<% end %>
+
+<%= turbo_stream.append "products" do %>
+ <%= render 'spree/shared/products', products: storefront_products.records %>
+<% end %>
diff --git a/storefront/config/routes.rb b/storefront/config/routes.rb
index 80dc893017..b46f7022ca 100644
--- a/storefront/config/routes.rb
+++ b/storefront/config/routes.rb
@@ -4,21 +4,6 @@ Spree::Core::Engine.add_routes do
get '/password', to: 'password#show', as: :password
post '/password', to: 'password#check', as: :check_password
- # Authentication with Devise
- if defined?(Devise)
- devise_for(
- Spree.user_class.model_name.singular_route_key,
- class_name: Spree.user_class.to_s,
- path: :user,
- controllers: {
- sessions: 'spree/user_sessions',
- passwords: 'spree/user_passwords',
- registrations: 'spree/user_registrations'
- },
- router_name: :spree
- )
- end
-
# Product Catalog
resources :products, only: [:index, :show], path: '/products' do
member do