Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spree search filter for properties and variants

I'm running Spree 1.3.1 and I'm trying to customize the Taxon show page.

I would like it to return the products contained inside the current Taxon, eventually filtered by a property or by an option value.

For example let's say that I'm seeing the Taxon of an underwear collection. I'd like to filter the products shown, by providing a certain size (option_type). In this case I should list only products that have variants with the requested size.

I would like also to be able to filter the products by the "fit" property. Filtering by the slip fit, I should be able to list only products inside the current Taxon that have the required property.

This is the Taxon controller show action:

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

How should I modify it to fit my needs?

like image 555
Augusto Avatar asked Feb 13 '13 11:02

Augusto


2 Answers

I partially solved the question.

I found out that I need to leave the controller as it is, the magic is done in the lib/spree/product_filters.rb file where I added this new product filter:

  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

Then I added the new filter to the Taxon model decorator with this:

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

Still I haven't found out how to create a filter for variants that have a specific option value.

like image 110
Augusto Avatar answered Oct 29 '22 19:10

Augusto


You talk about filtering on numerical value, I've written a filter for option ranges:

def ProductFilters.ov_range_test(range1, range2)
  ov = Arel::Table.new("spree_option_values")
  cast = Arel::Nodes::NamedFunction.new "CAST", [ ov[:presentation].as("integer")]
  comparaisons = cast.in(range1..range2)
  comparaisons
end

Spree::Product.add_search_scope :screenSize_range_any do |*opts|
  conds = opts.map {|o| Spree::ProductFilters.screenSize_filter[:conds][o]}.reject {|c| c.nil?}
  scope = conds.shift
  conds.each do |new_scope|
    scope = scope.or(new_scope)
  end
  option_values=Spree::OptionValue.where(scope).joins(:option_type).where(OptionType.table_name => {:name => "tailleEcran"}).pluck("#{OptionValue.table_name}.id")
  Spree::Product.where("#{Product.table_name}.id in (select product_id from #{Variant.table_name} v left join spree_option_values_variants ov on ov.variant_id = v.id where ov.option_value_id in (?))", option_values)
end

def ProductFilters.screenSize_filter
  conds = [ [ "20p ou moins",ov_range_test(0,20)],
  [ "20p - 30p",ov_range_test(20,30)],
  [ "30p - 40p" ,ov_range_test(30,40)],
  [ "40p ou plus",ov_range_test(40,190)]]
  { :name   => "taille",
    :scope  => :screenSize_range_any,
    :options => :tailleEcran,
    :conds  => Hash[*conds.flatten],
    :labels => conds.map {|k,v| [k,k]}
  }
end

You can see this one too, for discrete specific values: https://gist.github.com/Ranger-X/2511088

like image 24
hachpai Avatar answered Oct 29 '22 20:10

hachpai