Skip to content

Instantly share code, notes, and snippets.

@ashga
Created April 18, 2012 12:17
Show Gist options
  • Save ashga/2413198 to your computer and use it in GitHub Desktop.
Save ashga/2413198 to your computer and use it in GitHub Desktop.
Custom Spree filter code
module Spree
Spree::Product.class_eval do
def self.getprods(params)
#get taxon_id from permalink
taxonid = params[:id].blank? ? nil : Taxon.find_by_permalink!(params[:id]).id
#keywords for paging
keywords = params[:keywords]
per_page = params[:per_page].to_i
per_page = per_page > 0 ? per_page : Spree::Config[:products_per_page]
page = (params[:page].to_i <= 0) ? 1 : params[:page].to_i
sql = "SELECT p.* FROM spree_products p INNER JOIN spree_products_taxons t ON t.product_id = p.id INNER JOIN spree_taxons ON spree_taxons.id = t.taxon_id WHERE spree_taxons.id IN (#{taxonid}) "
#check if filter has been applied if so read data
params[:search].blank? ? nil : params[:search].each{ |p|
#get the proerty_id from the name supplied from the filter the offset => 1 is due to duplicated in the current data can be removed in most cases
tid = Spree::Property.find_by_name(p.first.gsub("_any", ""), :offset => 1).id
sql += "AND p.id IN (SELECT c.product_id FROM spree_product_properties c where c.property_id = #{tid} "
# allows multiple values for property_id
p.second.each_with_index{ |s,i|
if i == 0
sql += "AND c.value = '#{s.to_s}' "
else
sql += "OR c.value = '#{s.to_s}' "
end
}
sql += ")"
}
curr_page = keywords ? 1 : page
products = Spree::Product.active
products = products.find_by_sql(sql)
#products = products.page(curr_page).per(per_page)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment