Fast train

How to Move Taxons in Spree CommerceMarch 29, 2014

One problem I ran into creating my first site with the Spree shopping cart is that there is no way in the Spree admin interface to move a taxon with all of its contained products to a new parent taxonomy or taxon. Luckily this is very easy to do in the Rails console. First find the taxon object that you want to move:

$ taxon = Spree::Taxon.find_by_name("name")

And then you simply need to change two of its attributes, parent_id and taxonomy_id. First, get the destination parent taxon's id and then assign it to the parent_id attribute of the taxon you want to move:

$ parent_taxon = Spree::Taxon.find_by_name("parent_taxon_name")
$ taxon.parent_id = parent_taxon.id

Then assign the parent_taxon's taxonomy_id as the taxonomy_id of the taxon you want to move (all taxons under a Taxonomy have the same taxonomy_id):

$ child_taxonomy_id = parent_taxon.taxonomy_id
$ taxon.taxonomy_id = child_taxonomy_id

Every taxon must be organized under a taxonomy and will have that taxonomy's id as its taxonomy_id. This can (understandably) be a little confusing because every taxonomy is also a taxon so as a taxonomy it will have an id and as a taxon it will have a different id and a taxonomy_id, which refers to it's own id as a taxonomy. (KA-BOOM!)

Hope this helps someone.