api guide link: https://guides.spreecommerce.com/api/
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.
This can be the Category or the Brand it self.
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
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)
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
Hi,
in your taxon example you use:
and does not work, because
parent_id
expects an integer not an object, instead ofor even
should works.