Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spree grab all products for taxon and children taxons

I basically want to grab the following in a view:

All the products for the current taxon, and then all the products for the taxon's children. What's the best way to do this? Is there a method already?

like image 748
arachn1d Avatar asked Dec 28 '25 03:12

arachn1d


2 Answers

taxon_products = current_taxon.products
children_products = current_taxon.children.includes(:products).map(&:products).
                                  flatten.compact.uniq
like image 125
PinnyM Avatar answered Dec 30 '25 15:12

PinnyM


The answer by @PinnyM is fine if you only need to search one level of children, but if you want to find all the products under a taxon, you can use the in_taxon scope like this:

children_products = Spree::Product.in_taxon(current_taxon)
like image 33
Paul A Jungwirth Avatar answered Dec 30 '25 17:12

Paul A Jungwirth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!