2013-02-13 2 views
9

Spree 1.3.1을 실행 중이며 Taxon show 페이지를 사용자 정의하려고합니다.속성 및 변형에 대한 검색 필터가 희박합니다.

현재 Taxon 안에 포함 된 제품을 반환하고 결과적으로 속성이나 옵션 값으로 필터링하고 싶습니다.

예를 들어 내가 속옷 컬렉션의 Taxon을보고 있다고 가정 해 봅시다. 특정 크기 (option_type)를 제공하여 표시된 제품을 필터링하고 싶습니다. 이 경우 요청한 크기의 변형 제품 만 나열해야합니다.

"적합"속성으로 제품을 필터링 할 수도 있습니다. 슬립 핏 (Slit 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 

어떻게 내 요구에 맞게 수정해야합니까?

답변

7

나는 부분적으로 질문을 해결했다.

나는 그것이 마법 나는이 새로운 제품 필터를 추가 한 lib 디렉토리/마구/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 

을 내가 추가 이와 분류군 모델 장식에 새로운 필터 :

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 

여전히 나는 특정 옵션 값이 변형에 대한 필터를 만드는 방법을 발견하지 않았습니다. 당신은 수치 필터링에 대해 이야기

3

, 나는 옵션의 범위에 대한 필터를 작성했습니다 :

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 

당신은 이산 특정 값을 너무이 일을 볼 수 https://gist.github.com/Ranger-X/2511088

+0

안녕 얘들 아. 이 관습에 관한 문서가 유행하고 있습니까? 감사 :) . 나는 당신의 대답을 읽었지 만 나는 어떻게 정의하는지 이해할 수 없다. –

관련 문제