问题
I'd like to change the "products" overview of my Spree 3.0.x shop to show only products that have one or more variants that can be supplied. This would be on top of the normal scopes (available, with price in current currency). It would also affect the taxonomy-listings.
I don't want to affect the search page, though: when searching, unavailable products should show up.
As far as I can see, there is no such scope, neither on Spree:Product
nor on Spree::Product scopes.rb
.
If there is no such scope, what would be the proper chain of joins
- and includes
to write this scope on Product
?
回答1:
I'd probably start with something like:
Spree::Product.joins(
variants_including_master: :stock_items
).group('spree_products.id').having("SUM(count_on_hand) > 0")
That should give you close to what you're looking for.
回答2:
You can use this extension:
https://github.com/swrobel/spree_zero_stock_products/
By default it just adds a scope to Spree::Product
without changing the default search scope. You can use the provided :on_hand
scope at convenience.
来源:https://stackoverflow.com/questions/30399842/is-there-a-scope-to-allow-me-to-show-only-products-with-variants-with-stock