app/models/spree/product_import.rb
# create_variant_for
# This method assumes that some form of checking has already been done to
# make sure that we do actually want to create a variant.
# It performs a similar task to a product, but it also must pick up on
# size/color options
create_variant_for
This method assumes that some form of checking has already been done to
make sure that we do actually want to create a variant.
It performs a similar task to a product, but it also must pick up on
size/color options
create_variant_for This method assumes that some form of checking has already been done to make sure that we do actually want to create a variant . It performs a similar task to a product but it also must pick up on size / color options
def create_variant_for(product, options = {:with => {}})
      return if options[:with].nil?
      variant = Variant.find_by_sku(options[:with][:sku])
      raise SkuError, "SKU #{variant.sku} should belongs to #{product.inspect} but was #{variant.product.inspect}" if variant && variant.product != product
      if !variant
        variant = product.variants.new
        variant.id = options[:with][:id]
      else
        options[:with].delete(:id)
      end
      field = ProductImport.settings[:variant_comparator_field]
      log "VARIANT:: #{variant.inspect}  /// #{options.inspect } /// #{options[:with][field]} /// #{field}"
      options[:with][:price] = options[:with].delete(:price)
      options[:with].each do |field, value|
        variant.send("#{field}=", value) if variant.respond_to?("#{field}=")
        applicable_option_type = OptionType.where("lower(presentation) = ? OR lower(name) = ?",field.to_s, field.to_s).first
        if applicable_option_type.is_a?(OptionType)
          product.option_types << applicable_option_type unless product.option_types.include?(applicable_option_type)
          opt_value = applicable_option_type.option_values.where(["presentation = ? OR name = ?", value, value]).first
          opt_value = applicable_option_type.option_values.create(:presentation => value, :name => value) unless opt_value
          variant.option_values << opt_value unless variant.option_values.include?(opt_value)
        end
      end
      log "VARIANT PRICE #{variant.inspect} /// #{variant.price}"
      if variant.valid?
        variant.save
        ProductImport.settings[:taxonomy_fields].each do |field|
          associate_product_with_taxon(variant.product, field.to_s, options[:with][field.to_sym])
        end
        source_location = Spree::StockLocation.find_by(default: true)
        stock_item = variant.stock_items.where(stock_location_id: source_location.id).first
        if options[:with][:on_hand].nil?
          stock_item.set_count_on_hand(0)
        else
          stock_item.set_count_on_hand(options[:with][:on_hand])
        end
        ProductImport.settings[:image_fields].each do |field|
          find_and_attach_image_to(variant, options[:with][field.to_sym])
        end
        log("Variant of SKU #{variant.sku} successfully imported.\n")
      else
        log("A variant could not be imported - here is the information we have:\n" +
            "#{pp options[:with]}, #{variant.errors.full_messages.join(', ')}")
        return false
      end
    end
def create_variant_for ( product , options = { :with => { } } ) return if options [ :with ] . nil? variant = Variant . find_by_sku ( options [ :with ] [ :sku ] ) raise SkuError , "SKU #{variant.sku} should belongs to #{product.inspect} but was #{variant.product.inspect}" if variant && variant . product != product if ! variant variant = product . variants . new variant . id = options [ :with ] [ :id ] else options [ :with ] . delete ( :id ) end field = ProductImport . settings [ :variant_comparator_field ] log "VARIANT:: #{variant.inspect}  /// #{options.inspect } /// #{options[:with][field]} /// #{field}" options [ :with ] [ :price ] = options [ :with ] . delete ( :price ) options [ :with ] . each do | field , value | variant . send ( "#{field}=" , value ) if variant . respond_to? ( "#{field}=" ) applicable_option_type = OptionType . where ( "lower(presentation) = ? OR lower(name) = ?" , field . to_s , field . to_s ) . first if applicable_option_type . is_a? ( OptionType ) product . option_types << applicable_option_type unless product . option_types . include? ( applicable_option_type ) opt_value = applicable_option_type . option_values . where ( [ "presentation = ? OR name = ?" , value , value ] ) . first opt_value = applicable_option_type . option_values . create ( :presentation => value , :name => value ) unless opt_value variant . option_values << opt_value unless variant . option_values . include? ( opt_value ) end end log "VARIANT PRICE #{variant.inspect} /// #{variant.price}" if variant . valid? variant . save ProductImport . settings [ :taxonomy_fields ] . each do | field | associate_product_with_taxon ( variant . product , field . to_s , options [ :with ] [ field . to_sym ] ) end source_location = Spree :: StockLocation . find_by ( default : true ) stock_item = variant . stock_items . where ( stock_location_id : source_location . id ) . first if options [ :with ] [ :on_hand ] . nil? stock_item . set_count_on_hand ( 0 ) else stock_item . set_count_on_hand ( options [ :with ] [ :on_hand ] ) end ProductImport . settings [ :image_fields ] . each do | field | find_and_attach_image_to ( variant , options [ :with ] [ field . to_sym ] ) end log ( "Variant of SKU #{variant.sku} successfully imported.\n" ) else log ( "A variant could not be imported - here is the information we have:\n" + "#{pp options[:with]}, #{variant.errors.full_messages.join(', ')}" ) return false end end
create_variant_for
This method assumes that some form of checking has already been done to
make sure that we do actually want to create a variant.
create_variant_for This method assumes that some form of checking has already been done to make sure that we do actually want to create a variant .
# Just update variant if exists #Remap the options - oddly enough, Spree's product model has master_price and cost_price, while #variant has price and cost_price. #First, set the primitive fields on the object (prices, etc.) #Associate our new variant with any new taxonomies #Associate our new variant with any stock item #Finally, attach any images that have been specified #Log a success message
product
<not_specific>
options
<not_specific>