Spree product price range filter not working
Asked Answered
S

0

7

During my internship I needed to work on an existing ruby on rails project. This project is built with spree. The developers before my tweaked the price filter so that the range would change to to max of the products of that taxon. Here by the code of "lib/spree/core/product_filter.rb"

Spree::Product.add_search_scope :price_range_any do |*opts|
        opts_arr, max_prod, min_prod = [], nil, nil
        opts.each do |opt|
          tmp_arr = opt.split("+")
          opts_arr << tmp_arr[0]
          max_prod = tmp_arr[1]
          min_prod = tmp_arr[2]
        end
        conds = opts_arr.map {|o| Spree::Core::ProductFilters.price_filter(max_prod.to_i, min_prod.to_i)[:conds][o]}.reject { |c| c.nil? }
        scope = conds.shift
        conds.each do |new_scope|
          scope = scope.or(new_scope)
        end
        Spree::Product.joins(master: :default_price).where(scope)
      end

      def ProductFilters.format_price(amount)
        Spree::Money.new(amount)
      end

      def ProductFilters.price_filter(max_price = nil, min_price = nil)
        v = Spree::Price.arel_table
        if max_price < 200
          if max_price < 100
            highest_price = max_price - max_price.modulo(10)
          else
            highest_price = max_price - max_price.modulo(25)
          end
          interval = (highest_price)/5
          second = interval + interval
          third = second + interval

          conds = [ [ "#{format_price(0)} - #{format_price(interval)}"     , v[:amount].in(0..interval)],
                    [ "#{format_price(interval)} - #{format_price(second)}"        , v[:amount].in(interval..second)],
                    [ "#{format_price(second)} - #{format_price(third)}"        , v[:amount].in(second..third)],
                    [ "#{format_price(third)} - #{format_price(highest_price)}"        , v[:amount].in(third..highest_price)],
                    [ Spree.t(:or_over_price, price: format_price(highest_price)) , v[:amount].gteq(highest_price)]]
          {
            name:   Spree.t(:price_range),
            scope:  :price_range_any,
            conds:  Hash[*conds.flatten],
            labels: conds.map { |k,v| [k, k] }
          }
        else
          conds = [ [ "#{format_price(0)} - #{format_price(50)}"     , v[:amount].in(0..50)],
                    [ "#{format_price(50)} - #{format_price(100)}"        , v[:amount].in(50..100)],
                    [ "#{format_price(100)} - #{format_price(150)}"        , v[:amount].in(100..150)],
                    [ "#{format_price(150)} - #{format_price(200)}"        , v[:amount].in(150..200)],
                    [ Spree.t(:or_over_price, price: format_price(200)) , v[:amount].gteq(200)]]
          {
            name:   Spree.t(:price_range),
            scope:  :price_range_any,
            conds:  Hash[*conds.flatten],
            labels: conds.map { |k,v| [k, k] }
          }
        end
      end

It is working, in the price range filter the range changed when the max price went up but the filter doesn't filter the products.

I tried everything I know so far of ruby on rails but nothing seem to be helping. Example when I comment key value of the add_search_scope out nothing changes.

Can you please tell me whats going wrong?

Snubnosed answered 15/7, 2015 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.