app/models/spree/product_import.rb
# associate_product_with_taxon
# This method accepts three formats of taxon hierarchy strings which will
# associate the given products with taxons:
# 1. A string on it's own will will just find or create the taxon and
# add the product to it. e.g. taxonomy = "Category", taxon_hierarchy = "Tools" will
# add the product to the 'Tools' category.
# 2. A item > item > item structured string will read this like a tree - allowing
# a particular taxon to be picked out
# 3. An item > item & item > item will work as above, but will associate multiple
# taxons with that product. This form should also work with format 1.
associate_product_with_taxon
This method accepts three formats of taxon hierarchy strings which will
associate the given products with taxons:
1. A string on it's own will will just find or create the taxon and
add the product to it.
associate_product_with_taxon This method accepts three formats of taxon hierarchy strings which will associate the given products with taxons : 1 . A string on it ' s own will will just find or create the taxon and add the product to it .
def associate_product_with_taxon(product, taxonomy, taxon_hierarchy)
      return if product.nil? || taxonomy.nil? || taxon_hierarchy.nil?
      taxon_hierarchy.split(/\s*\|\s*/).each do |hierarchy|
        hierarchy = hierarchy.split(/\s*>\s*/)
        taxonomy = Spree::Taxonomy.where("lower(name) = ?", hierarchy.first.downcase).first
        taxonomy = Taxonomy.create(:name => hierarchy.first.capitalize) if taxonomy.nil? && ProductImport.settings[:create_missing_taxonomies]
        last_taxon = taxonomy.root
        product.taxons << last_taxon unless product.taxons.include?(last_taxon)
        hierarchy.shift
        hierarchy.each do |taxon|
          taxon = taxon.titleize
          last_taxon = last_taxon.children.find_or_create_by(name: taxon, taxonomy_id: taxonomy.id)
          product.taxons << last_taxon unless product.taxons.include?(last_taxon)
        end
      end
    end
def associate_product_with_taxon ( product , taxonomy , taxon_hierarchy ) return if product . nil? || taxonomy . nil? || taxon_hierarchy . nil? taxon_hierarchy . split ( / \s * \| \s * / ) . each do | hierarchy | hierarchy = hierarchy . split ( / \s *> \s * / ) taxonomy = Spree :: Taxonomy . where ( "lower(name) = ?" , hierarchy . first . downcase ) . first taxonomy = Taxonomy . create ( :name => hierarchy . first . capitalize ) if taxonomy . nil? && ProductImport . settings [ :create_missing_taxonomies ] last_taxon = taxonomy . root product . taxons << last_taxon unless product . taxons . include? ( last_taxon ) hierarchy . shift hierarchy . each do | taxon | taxon = taxon . titleize last_taxon = last_taxon . children . find_or_create_by ( name : taxon , taxonomy_id : taxonomy . id ) product . taxons << last_taxon unless product . taxons . include? ( last_taxon ) end end end
associate_product_with_taxon
This method accepts three formats of taxon hierarchy strings which will
associate the given products with taxons:
1.
associate_product_with_taxon This method accepts three formats of taxon hierarchy strings which will associate the given products with taxons : 1 .
#Using find_or_create_by_name is more elegant, but our magical params code automatically downcases # the taxonomy name, so unless we are using MySQL, this isn't going to work. # taxonomy_name = taxonomy # taxonomy = Taxonomy.find(:first, :conditions => ["lower(name) = ?", taxonomy]) # taxonomy = Taxonomy.create(:name => taxonomy_name.capitalize) if taxonomy.nil? && ProductImport.settings[:create_missing_taxonomies] #last_taxon = last_taxon.children.find_or_create_by_name_and_taxonomy_id(taxon, taxonomy.id)
product
<not_specific>
taxonomy
<not_specific>
taxon_hierarchy
<not_specific>