Is there a scope to allow me to show only products with variants with stock?

孤者浪人 提交于 2019-12-24 15:33:20

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!