Attempting to create a filter for the taxon pages ...
# support
t
Attempting to create a filter for the taxon pages to filter products based on the product properties, and what we currently have can filter against individual properties (select as many values under the same property name as desired and it will filter), but when trying to filter against more than one property name, we come back with zero results, which I presume is due to the way these methods are defined in
product_filters.rb
. I found a similar issue here but it was for Spree and it's pretty old. So far I haven't been able to adapt this code to work with current Solidus. We do have a custom searcher based off of
Spree::Core::Search::Base
already in place, so I have been trying to override
add_search_scopes
method there. Any recommendations on where to focus my efforts or if there's a better way to go about this?
s
Yes, if you look at the core product_filters, it’s recommended to override the file. If you look at the “brand” filters in there, it’s possible to copy and rename them to do what you need. To make something like
brand_any
and
flavor_any
chainable, this seems to work:
Copy code
-          Spree::Product.with_property('brand').where(scope)
+          where(id: Spree::Product.with_property('brand').where(scope).pluck(:id))
I’m sure that’s not great because you’re doing a subselect in there but it should move you along. You might also find that search returns duplicate products since there will be cross-talk in your properties, but you can clean that up in the search lib. I’m not sure if you’d need to override add_search_scopes since that should just be chaining scopes if Product responds to the method.
t
Thanks. That helps knowing which of these to focus on. This is what we have working in
product_filters.rb
currently and what is working for individual properties. It just returns nothing when multiple separate properties are selected. I'm guessing due to the chaining, but I haven't been able to pinpoint where. Changing line 45 to
Spree::Product.with_property(property.name).where(scope).where(id: Spree::Product.with_property(property.name)).where(scope).pluck(:id)
causes an error `undefined method
preload' for []:Array
s
Yup you may want to play around in console, or write some specs to sort of tease out what the code is doing, but in
Spree::Product.with_property(property.name).where(scope).where(id: Spree::Product.with_property(property.name)).where(scope).pluck(:id)
I think you just want
Spree::Product.where(id: Spree::Product.with_property(property.name)).where(scope).pluck(:id)