app/models/spree/product_import.rb
# create_product_using
# This method performs the meaty bit of the import - taking the parameters for the
# product we have gathered, and creating the product and related objects.
# It also logs throughout the method to try and give some indication of process.
create_product_using
This method performs the meaty bit of the import - taking the parameters for the
product we have gathered, and creating the product and related objects.
It also logs throughout the method to try and give some indication of process.
create_product_using This method performs the meaty bit of the import - taking the parameters for the product we have gathered and creating the product and related objects . It also logs throughout the method to try and give some indication of process .
def create_product_using(params_hash)
      product         = Product.new
      properties_hash = Hash.new
      special_fields  = ProductImport.settings.values_at(
                          :image_fields,
                          :taxonomy_fields,
                          :store_field,
                          :variant_comparator_field
                        ).flatten.map(&:to_s)
      params_hash.each do |field, value|
        if product.respond_to?("#{field}=")
          product.send("#{field}=", value)
        elsif not special_fields.include?(field.to_s) and property = Property.where("lower(name) = ?", field).first
          properties_hash[property] = value
        end
      end
      after_product_built(product, params_hash)
      unless product.valid?
        log(msg = "A product could not be imported - here is the information we have:\n" +
            "#{pp params_hash}, #{product.errors.full_messages.join(', ')}")
        raise ProductError, msg
      end
      log(product.name)
      p = Spree::Variant.find_by_sku(product.sku)
      if @skus_of_products_before_import.include? product.sku and p.deleted_at.nil?
        log("#{product.name} is already in the system and active.\n")
      else
        if !p.nil? && !p.deleted_at.nil?
          p.destroy
          log("#{product.name} was removed from the system and will be replaced.\n")
        end
        product.save and product_ids << product.id
        log("Saved object before creating associated objects for: #{product.name}")
        properties_hash.each do |property, value|
          product_property = Spree::ProductProperty.where(:product_id => product.id, :property_id => property.id).first_or_initialize
          product_property.value = value
          product_property.save!
        end
        ProductImport.settings[:taxonomy_fields].each do |field|
          associate_product_with_taxon(product, field.to_s, params_hash[field.to_sym])
        end
        ProductImport.settings[:image_fields].each do |field|
          find_and_attach_image_to(product, params_hash[field.to_sym])
        end
        if ProductImport.settings[:multi_domain_importing] && product.respond_to?(:stores)
          begin
            store = Store.find(
              :first,
              :conditions => ["id = ? OR code = ?",
                params_hash[ProductImport.settings[:store_field]],
                params_hash[ProductImport.settings[:store_field]]
              ]
            )
            product.stores << store
          rescue
            log("#{product.name} could not be associated with a store. Ensure that Spree's multi_domain extension is installed and that fields are mapped to the CSV correctly.")
          end
        end
        source_location = Spree::StockLocation.find_by(default: true)
        stock_item = product.stock_items.where(stock_location_id: source_location.id).first
        if params_hash[:on_hand].nil?
          stock_item.set_count_on_hand(0)
        else
          stock_item.set_count_on_hand(params_hash[:on_hand])
        end
        log("#{product.name} successfully imported.\n")
      end
      return true
    end
def create_product_using ( params_hash ) product = Product . new properties_hash = Hash . new special_fields = ProductImport . settings . values_at ( :image_fields , :taxonomy_fields , :store_field , :variant_comparator_field ) . flatten . map ( & :to_s ) params_hash . each do | field , value | if product . respond_to? ( "#{field}=" ) product . send ( "#{field}=" , value ) elsif not special_fields . include? ( field . to_s ) and property = Property . where ( "lower(name) = ?" , field ) . first properties_hash [ property ] = value end end after_product_built ( product , params_hash ) unless product . valid? log ( msg = "A product could not be imported - here is the information we have:\n" + "#{pp params_hash}, #{product.errors.full_messages.join(', ')}" ) raise ProductError , msg end log ( product . name ) p = Spree :: Variant . find_by_sku ( product . sku ) if @skus_of_products_before_import . include? product . sku and p . deleted_at . nil? log ( "#{product.name} is already in the system and active.\n" ) else if ! p . nil? && ! p . deleted_at . nil? p . destroy log ( "#{product.name} was removed from the system and will be replaced.\n" ) end product . save and product_ids << product . id log ( "Saved object before creating associated objects for: #{product.name}" ) properties_hash . each do | property , value | product_property = Spree :: ProductProperty . where ( :product_id => product . id , :property_id => property . id ) . first_or_initialize product_property . value = value product_property . save! end ProductImport . settings [ :taxonomy_fields ] . each do | field | associate_product_with_taxon ( product , field . to_s , params_hash [ field . to_sym ] ) end ProductImport . settings [ :image_fields ] . each do | field | find_and_attach_image_to ( product , params_hash [ field . to_sym ] ) end if ProductImport . settings [ :multi_domain_importing ] && product . respond_to? ( :stores ) begin store = Store . find ( :first , :conditions => [ "id = ? OR code = ?" , params_hash [ ProductImport . settings [ :store_field ] ] , params_hash [ ProductImport . settings [ :store_field ] ] ] ) product . stores << store rescue log ( "#{product.name} could not be associated with a store. Ensure that Spree's multi_domain extension is installed and that fields are mapped to the CSV correctly." ) end end source_location = Spree :: StockLocation . find_by ( default : true ) stock_item = product . stock_items . where ( stock_location_id : source_location . id ) . first if params_hash [ :on_hand ] . nil? stock_item . set_count_on_hand ( 0 ) else stock_item . set_count_on_hand ( params_hash [ :on_hand ] ) end log ( "#{product.name} successfully imported.\n" ) end return true end
create_product_using
This method performs the meaty bit of the import - taking the parameters for the
product we have gathered, and creating the product and related objects.
create_product_using This method performs the meaty bit of the import - taking the parameters for the product we have gathered and creating the product and related objects .
# Array of special fields. Prevent adding them to properties. #The product is inclined to complain if we just dump all params # into the product (including images and taxonomies). # What this does is only assigns values to products if the product accepts that field. #We can't continue without a valid product here #Just log which product we're processing #This should be caught by code in the main import code that checks whether to create #variants or not. Since that check can be turned off, however, we should double check. #Save the object before creating asssociated objects #Associate properties with product #Associate our new product with any taxonomies that we need to worry about #Finally, attach any images that have been specified #Stock item
params_hash
<not_specific>