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.

Display only Store products in the Frontend

Damian Legawiece72ced9330e3
diff --git a/core/app/helpers/spree/products_helper.rb b/core/app/helpers/spree/products_helper.rb
index 12be92afac..c35b6d994c 100644
--- a/core/app/helpers/spree/products_helper.rb
+++ b/core/app/helpers/spree/products_helper.rb
@@ -131,7 +131,7 @@ module Spree
return [] if product_ids.empty?
- Spree::Product.
+ current_store.products.
available.not_discontinued.distinct.
where(id: product_ids).
includes(
diff --git a/core/lib/spree/core/controller_helpers/search.rb b/core/lib/spree/core/controller_helpers/search.rb
index 5c09ccf900..5f56146c74 100644
--- a/core/lib/spree/core/controller_helpers/search.rb
+++ b/core/lib/spree/core/controller_helpers/search.rb
@@ -6,6 +6,7 @@ module Spree
Spree::Config.searcher_class.new(params).tap do |searcher|
searcher.current_user = try_spree_current_user
searcher.current_currency = current_currency&.upcase
+ searcher.current_store = current_store
end
end
end
diff --git a/core/lib/spree/core/search/base.rb b/core/lib/spree/core/search/base.rb
index 568f9a94e1..84412afcca 100644
--- a/core/lib/spree/core/search/base.rb
+++ b/core/lib/spree/core/search/base.rb
@@ -7,7 +7,7 @@ module Spree
def initialize(params)
@properties = {}
@current_currency = Spree::Config[:currency]
- @current_store = params[:current_store]
+ @current_store = params[:current_store] || Spree::Store.default
@taxon = params[:taxon]
prepare(params)
@@ -28,8 +28,7 @@ module Spree
protected
def extended_base_scope
- base_scope = Spree::Product.spree_base_scopes
- base_scope = base_scope.by_store(current_store) if current_store.present?
+ base_scope = current_store.products.spree_base_scopes
base_scope = get_products_conditions_for(base_scope, keywords)
base_scope = Spree::Dependencies.products_finder.constantize.new(
scope: base_scope,
diff --git a/core/spec/lib/search/base_spec.rb b/core/spec/lib/search/base_spec.rb
index fc5ad6165e..348d990fc8 100644
--- a/core/spec/lib/search/base_spec.rb
+++ b/core/spec/lib/search/base_spec.rb
@@ -105,13 +105,13 @@ describe Spree::Core::Search::Base do
let!(:products_for_eu_store) do
(1..3).map do |v|
- create(:product, name: "P-EU-#{v}", stores: [eu_store])
+ create(:product, name: "P-EU-#{v}", stores: [eu_store], currency: 'EUR')
end
end
let!(:products_for_uk_store) do
(1..2).map do |v|
- create(:product, name: "P-UK-#{v}", stores: [uk_store])
+ create(:product, name: "P-UK-#{v}", stores: [uk_store], currency: 'GBP')
end
end
@@ -156,22 +156,5 @@ describe Spree::Core::Search::Base do
end
end
end
-
- context 'current store is not given in params' do
- let(:params) { { current_store: nil } }
-
- it 'returns 8 products' do
- expect(retrieved_products.count).to eq(8)
- end
-
- it 'returns products from all stores' do
- expect(retrieved_products).to contain_exactly(
- product1, product2,
- product_for_spree_store,
- *products_for_eu_store,
- *products_for_uk_store
- )
- end
- end
end
end
diff --git a/frontend/app/controllers/spree/products_controller.rb b/frontend/app/controllers/spree/products_controller.rb
index 183762a2aa..67bf6e3263 100644
--- a/frontend/app/controllers/spree/products_controller.rb
+++ b/frontend/app/controllers/spree/products_controller.rb
@@ -54,7 +54,7 @@ module Spree
end
def load_product
- @product = Product.for_user(try_spree_current_user).friendly.find(params[:id])
+ @product = current_store.products.for_user(try_spree_current_user).friendly.find(params[:id])
end
def load_taxon
diff --git a/frontend/spec/controllers/spree/products_controller_spec.rb b/frontend/spec/controllers/spree/products_controller_spec.rb
index 17920f317b..6de3eafce7 100644
--- a/frontend/spec/controllers/spree/products_controller_spec.rb
+++ b/frontend/spec/controllers/spree/products_controller_spec.rb
@@ -2,6 +2,7 @@ require 'spec_helper'
describe Spree::ProductsController, type: :controller do
let!(:product) { create(:product, available_on: 1.year.from_now) }
+ let(:product_from_other_store) { create(:product, stores: [create(:store)]) }
let(:taxon) { create(:taxon) }
# Regression test for #1390
@@ -15,6 +16,10 @@ describe Spree::ProductsController, type: :controller do
expect { get :show, params: { id: product.to_param } }.to raise_error(ActiveRecord::RecordNotFound)
end
+ it 'cannot view products from other store' do
+ expect { get :show, params: { id: product_from_other_store.to_param } }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+
it 'provides the current user to the searcher class' do
user = mock_model(Spree.user_class, last_incomplete_spree_order: nil, spree_api_key: 'fake')
allow(controller).to receive_messages spree_current_user: user