Skip to content

Instantly share code, notes, and snippets.

@Humoud
Last active February 3, 2023 14:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Humoud/1b1d329cf43d2b25588d to your computer and use it in GitHub Desktop.
Save Humoud/1b1d329cf43d2b25588d to your computer and use it in GitHub Desktop.
A cheat sheet for the models of Spree. Spree is a complete open source e-commerce solution for Ruby on Rails.

api guide link: https://guides.spreecommerce.com/api/

===== Taxonomies ======

useful link: https://guides.spreecommerce.com/user/configuring_taxonomies.html

Taxonomies are the Spree system’s approach to category trees. The heading of a tree is called a Taxonomy. Any child branches are called Taxons. Taxons themselves can have their own branches.

Taxonomy represents an entire tree, whereas Taxons are the nodes that make up that tree.

Examples of Taxonomies: Category, Brand, etc

To create a Taxonomy:

t = Spree::Taxonomy.create(name: "Season")
t.root # => auto-gen root Taxon because we created without one.

====== Taxons ========

This can be the Category or the Brand it self.

Note that the taxon object is tied to the products object .ie there is a relations.
tt = Spree::Taxon.create(name: "Winter", taxonomy_id: t.id, parent_id: t.root)
Where t.root is the root taxon id
And   t.id is the taxonomy id

====== Products =======

p1 = Spree::Product.create(name: "Silver Lace", price: 12.0, taxon_ids: [14], shipping_category_id: 1, available_on: Date.today)

p2 = Spree::Product.create(name: "Pearly Watch", price: 250.0, taxon_ids: [tt.id], shipping_category_id: 1, available_on: Date.today)

====== Images =======

def image(name, type="png")
  images_path = Pathname.new(File.dirname(__FILE__)) + "images"
  path = images_path + "#{name}.#{type}"
  return false if !File.exist?(path)
  File.open(path)
end

# Assumes that the name of the image is similar to
# the name of the product.
images = Spree::Product.all.map{ |product|
  {
    product.master => [ { :attachment => image( product.name ) } ]
  }
}

images.each do |entry|
  variant = entry.keys[0]
  attachment = entry.values[0][0]
  puts "Loading images for #{variant.product.name}"
  variant.images.create!(attachment)
end
@leite
Copy link

leite commented Apr 25, 2018

Hi,

in your taxon example you use:

tt = Spree::Taxon.create(name: "Winter", taxonomy_id: t.id, parent_id: t.root)

and does not work, because parent_id expects an integer not an object, instead of

tt = Spree::Taxon.create(name: "Winter", taxonomy_id: t.id, parent_id: t.root.id)

or even

tt = Spree::Taxon.create(name: "Winter", taxonomy: t, parent: t.root)

should works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment