Skip to content

Commit

Permalink
allow use an array for line_items on Importer::Order
Browse files Browse the repository at this point in the history
  • Loading branch information
ccarruitero committed Jun 8, 2020
1 parent 448d504 commit 9bb8b96
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
34 changes: 23 additions & 11 deletions core/lib/spree/core/importer/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def self.create_shipments_from_params(shipments_hash, order)
shipment = Shipment.new
shipment.tracking = target[:tracking]
shipment.stock_location = Spree::StockLocation.find_by(id: target[:stock_location_id]) ||
Spree::StockLocation.find_by(admin_name: target[:stock_location]) ||
Spree::StockLocation.find_by!(name: target[:stock_location])
Spree::StockLocation.find_by(admin_name: target[:stock_location]) ||
Spree::StockLocation.find_by!(name: target[:stock_location])

inventory_units = target[:inventory_units] || []
inventory_units.each do |inventory_unit|
Expand Down Expand Up @@ -100,19 +100,31 @@ def self.create_shipments_from_params(shipments_hash, order)

def self.create_line_items_from_params(line_items_hash, order)
return {} unless line_items_hash
line_items_hash.each_key do |key|
extra_params = line_items_hash[key].except(:variant_id, :quantity, :sku)
line_item = ensure_variant_id_from_params(line_items_hash[key])
line_item = order.contents.add(Spree::Variant.find(line_item[:variant_id]), line_item[:quantity])
# Raise any errors with saving to prevent import succeeding with line items failing silently.
if extra_params.present?
line_item.update!(extra_params)
else
line_item.save!

case line_items_hash
when Hash
line_items_hash.each_key do |key|
create_line_item(line_items_hash[key], order)
end
when Array
line_items_hash.each do |line_item_hash|
create_line_item(line_item_hash, order)
end
end
end

def self.create_line_item(line_item_hash, order)
extra_params = line_item_hash.except(:variant_id, :quantity, :sku)
line_item = ensure_variant_id_from_params(line_item_hash)
line_item = order.contents.add(Spree::Variant.find(line_item[:variant_id]), line_item[:quantity])
# Raise any errors with saving to prevent import succeeding with line items failing silently.
if extra_params.present?
line_item.update!(extra_params)
else
line_item.save!
end
end

def self.create_adjustments_from_params(adjustments, order)
return [] unless adjustments
adjustments.each do |target|
Expand Down
13 changes: 13 additions & 0 deletions core/spec/lib/spree/core/importer/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ module Core
expect(line_item.quantity).to eq(5)
end

it 'handle when line items is an array' do
params = {
line_items_attributes: [
{ variant_id: variant_id, quantity: 7 }
]
}
order = Importer::Order.import(user, params)

line_item = order.line_items.first
expect(line_item.variant_id).to eq(variant_id)
expect(line_item.quantity).to eq(7)
end

it 'can build an order from API shipping address' do
params = { ship_address_attributes: ship_address,
line_items_attributes: line_items }
Expand Down

0 comments on commit 9bb8b96

Please sign in to comment.