lib/loaders/spree/shopify_order_migrator.rb
def process_line_item( row, order )
        # for now just hard coding the columns 16 (quantity) and 17 (variant name), 20 (variant sku)
        @quantity_header_idx ||= 16
        @price_header_idx ||= 18
        @sku_header_idx ||= 20      # Lineitem:sku

        # if by name ...
        # by name - product = Spree::Product.where(:name => row[17]).first
        # variant = product.master if(product)

        sku = row[@sku_header_idx]

        method_detail = DataShift::MethodDictionary.find_method_detail(Spree::Order, "LineItems" )

        # will perform substitutions etc
        @populator.prepare_data(method_detail, sku)

        variant = Spree::Variant.where(:sku => sku).first

        unless(variant)
          raise RecordNotFound.new("Failed to find Variant with sku [#{sku}] for LineItem")
        end

        logger.info("Processing LineItem - Found Variant [#{variant.sku}] (#{variant.name})") if(variant)

        quantity = row[@quantity_header_idx].to_i

        if(quantity > 0)

          price = row[@price_header_idx].to_f

          logger.info("Adding LineItem for #{sku} with Quantity #{quantity} to Order #{load_object.inspect}")

          if(order.new_record?)
            puts "ORDER NEEDS SAVING - #{order.number} (#{order.id})"
            save
          end

          # idea incase we need full stock management
          # variant.stock_items.first.adjust_count_on_hand(quantity)

          begin
            #puts("Adding LineItem for #{sku} with Quantity #{quantity} to Order #{load_object.inspect}")
            #puts("Variant [#{variant.sku}] (#{variant.name})") if(variant)

            logger.info("Attempting to add new LineItem against Order #{order.number} (#{order.id})")

            order.line_items.new(quantity: quantity, variant: variant, :price => price)
=begin

            line_item = Spree::LineItem.create!(:variant => variant,
                                            :quantity => quantity,
                                            :price => price,
                                            :pre_tax_amount => price,
                                            :order => order,
                                            :currency => order.currency)


             # line_item.save
              #order.reload

              logger.info("Success - Added LineItems to Order #{order.number} (#{order.id})")
            end
=end
          rescue => e
            puts("Create LineItem failed for [#{sku}] Order #{order.number} (#{order.id}) - #{e.inspect}")
            puts("Create LineItem failed for [#{sku}] Order #{order.number} (#{order.id}) - #{e.inspect}")
            logger.error("Create LineItem failed for [#{sku}] Order #{order.number} (#{order.id}) - #{e.inspect}")
            raise
          end

        end
      end
# Options
#
#  :reload           : Force load of the method dictionary for object_class even if already loaded
#  :verbose          : Verbose logging and to STDOUT
#
Options
:reload           : Force load of the method dictionary for object_class even if already loaded
:verbose          : Verbose logging and to STDOUT
Options : reload : Force load of the method dictionary for object_class even if already loaded : verbose : Verbose logging and to STDOUT
def process_line_item( row, order )
        @quantity_header_idx ||= 16
        @price_header_idx ||= 18
        @sku_header_idx ||= 20      
        sku = row[@sku_header_idx]
        method_detail = DataShift::MethodDictionary.find_method_detail(Spree::Order, "LineItems" )
        @populator.prepare_data(method_detail, sku)
        variant = Spree::Variant.where(:sku => sku).first
        unless(variant)
          raise RecordNotFound.new("Failed to find Variant with sku [#{sku}] for LineItem")
        end
        logger.info("Processing LineItem - Found Variant [#{variant.sku}] (#{variant.name})") if(variant)
        quantity = row[@quantity_header_idx].to_i
        if(quantity > 0)
          price = row[@price_header_idx].to_f
          logger.info("Adding LineItem for #{sku} with Quantity #{quantity} to Order #{load_object.inspect}")
          if(order.new_record?)
            puts "ORDER NEEDS SAVING - #{order.number} (#{order.id})"
            save
          end
          begin
            logger.info("Attempting to add new LineItem against Order #{order.number} (#{order.id})")
            order.line_items.new(quantity: quantity, variant: variant, :price => price)
          rescue => e
            puts("Create LineItem failed for [#{sku}] Order #{order.number} (#{order.id}) - #{e.inspect}")
            puts("Create LineItem failed for [#{sku}] Order #{order.number} (#{order.id}) - #{e.inspect}")
            logger.error("Create LineItem failed for [#{sku}] Order #{order.number} (#{order.id}) - #{e.inspect}")
            raise
          end
        end
      end
def process_line_item ( row , order ) @quantity_header_idx ||= 16 @price_header_idx ||= 18 @sku_header_idx ||= 20 sku = row [ @sku_header_idx ] method_detail = DataShift :: MethodDictionary . find_method_detail ( Spree :: Order , "LineItems" ) @populator . prepare_data ( method_detail , sku ) variant = Spree :: Variant . where ( :sku => sku ) . first unless ( variant ) raise RecordNotFound . new ( "Failed to find Variant with sku [#{sku}] for LineItem" ) end logger . info ( "Processing LineItem - Found Variant [#{variant.sku}] (#{variant.name})" ) if ( variant ) quantity = row [ @quantity_header_idx ] . to_i if ( quantity > 0 ) price = row [ @price_header_idx ] . to_f logger . info ( "Adding LineItem for #{sku} with Quantity #{quantity} to Order #{load_object.inspect}" ) if ( order . new_record? ) puts "ORDER NEEDS SAVING - #{order.number} (#{order.id})" save end begin logger . info ( "Attempting to add new LineItem against Order #{order.number} (#{order.id})" ) order . line_items . new ( quantity : quantity , variant : variant , :price => price ) rescue => e puts ( "Create LineItem failed for [#{sku}] Order #{order.number} (#{order.id}) - #{e.inspect}" ) puts ( "Create LineItem failed for [#{sku}] Order #{order.number} (#{order.id}) - #{e.inspect}" ) logger . error ( "Create LineItem failed for [#{sku}] Order #{order.number} (#{order.id}) - #{e.inspect}" ) raise end end end
Options
:reload           : Force load of the method dictionary for object_class even if already loaded
:verbose          : Verbose logging and to STDOUT
Options : reload : Force load of the method dictionary for object_class even if already loaded : verbose : Verbose logging and to STDOUT
# for now just hard coding the columns 16 (quantity) and 17 (variant name), 20 (variant sku) # Lineitem:sku # if by name ... # by name - product = Spree::Product.where(:name => row[17]).first # variant = product.master if(product) # will perform substitutions etc # idea incase we need full stock management # variant.stock_items.first.adjust_count_on_hand(quantity) #puts("Adding LineItem for #{sku} with Quantity #{quantity} to Order #{load_object.inspect}") #puts("Variant [#{variant.sku}] (#{variant.name})") if(variant) =begin

            line_item = Spree::LineItem.create!(:variant => variant,
                                            :quantity => quantity,
                                            :price => price,
                                            :pre_tax_amount => price,
                                            :order => order,
                                            :currency => order.currency)


             # line_item.save
              #order.reload

              logger.info("Success - Added LineItems to Order #{order.number} (#{order.id})")
            end
=end
row
order