Adam
Adam

Reputation: 464

How to show products based on taxons & user roles

I have a spree site where there are multiple different user roles. Some categories of products are only available to certain users (eg. Wholesale customers can access wholesale products). How would I limit this? I tried to do it using

<% if spree_current_user.has_spree_role?("wholesale") %>

<% @products = Spree::Product.find_by_sql('SELECT sp.* FROM spree_products sp LEFT JOIN spree_products_taxons spt ON spt.product_id = sp.id LEFT JOIN spree_taxons st ON spt.taxon_id = st.id WHERE st.name = "Wholesale"') %>

in the products partial where it shows the products but this overrides the retrieve_products method so it needs to be done somewhere earlier. I tried it in the /lib/spree/core/search/base.rb retrieve_products but this doesn't recognize spree_current_user...

Sorry for being a noob. Any tips would be much appreciated.

Upvotes: 0

Views: 95

Answers (1)

Bartłomiej Gładys
Bartłomiej Gładys

Reputation: 4615

Firstly you must create extension

extension guides

After under your extension create /controllers/spree/home_controller_decorator.rb

In this file put:

module Spree
  HomeController.class_eval do
    def index
     @retrieve_prodcuts = # type here your @products code
    end
  end
end

Similarly you can do this also for products_controlleror other controllers.

Upvotes: 1

Related Questions