Aug*_*sto 9 ruby-on-rails spree
我正在运行Spree 1.3.1,我正在尝试自定义Taxon show页面.
我希望它返回当前Taxon中包含的产品,最终通过属性或选项值进行过滤.
例如,假设我正在看内衣系列的Taxon.我想过滤所显示的产品,提供一定的尺寸(option_type).在这种情况下,我应该只列出具有所需大小的变体的产品.
我还希望能够通过"fit"属性过滤产品.通过滑动过滤,我应该只能列出当前分类中具有所需属性的产品.
这是Taxon控制器显示动作:
Spree::TaxonsController.class_eval do
def show
@taxon = Spree::Taxon.find_by_permalink!(params[:id])
return unless @taxon
@searcher = Spree::Config.searcher_class.new(params)
@searcher.current_user = try_spree_current_user
@searcher.current_currency = current_currency
@products = @searcher.retrieve_products
respond_with(@taxon)
end
end
Run Code Online (Sandbox Code Playgroud)
我该如何修改它以满足我的需求?
我部分解决了这个问题.
我发现我需要按原样离开控制器,魔术是在lib/spree/product_filters.rb文件中完成的,我添加了这个新的产品过滤器:
if Spree::Property.table_exists?
Spree::Product.add_search_scope :fit_any do |*opts|
conds = opts.map {|o| ProductFilters.fit_filter[:conds][o]}.reject {|c| c.nil?}
scope = conds.shift
conds.each do |new_scope|
scope = scope.or(new_scope)
end
Spree::Product.with_property("fit").where(scope)
end
def ProductFilters.fit_filter
fit_property = Spree::Property.find_by_name("fit")
fits = Spree::ProductProperty.where(:property_id => fit_property).pluck(:value).uniq
pp = Spree::ProductProperty.arel_table
conds = Hash[*fits.map { |b| [b, pp[:value].eq(b)] }.flatten]
{ :name => "Fits",
:scope => :fit_any,
:conds => conds,
:labels => (fits.sort).map { |k| [k, k] }
}
end
end
Run Code Online (Sandbox Code Playgroud)
然后我将新过滤器添加到Taxon模型装饰器中:
Spree::Taxon.class_eval do
def applicable_filters
fs = []
fs << Spree::Core::ProductFilters.fit_filter if Spree::Core::ProductFilters.respond_to?(:fit_filter)
fs
end
end
Run Code Online (Sandbox Code Playgroud)
我还没有找到如何为具有特定选项值的变体创建过滤器.