diff --git a/core/app/models/spree/order_updater.rb b/core/app/models/spree/order_updater.rb index f8ef2f94949..adfbdae0050 100644 --- a/core/app/models/spree/order_updater.rb +++ b/core/app/models/spree/order_updater.rb @@ -97,20 +97,7 @@ def update_item_total end def persist_totals - order.update_columns( - payment_state: order.payment_state, - shipment_state: order.shipment_state, - item_total: order.item_total, - item_count: order.item_count, - adjustment_total: order.adjustment_total, - included_tax_total: order.included_tax_total, - additional_tax_total: order.additional_tax_total, - payment_total: order.payment_total, - shipment_total: order.shipment_total, - promo_total: order.promo_total, - total: order.total, - updated_at: Time.current, - ) + order.save!(validate: false) end # Updates the +shipment_state+ attribute according to the following logic: diff --git a/core/lib/spree/core/importer/order.rb b/core/lib/spree/core/importer/order.rb index 6097cf02f03..29ab9668b31 100644 --- a/core/lib/spree/core/importer/order.rb +++ b/core/lib/spree/core/importer/order.rb @@ -4,7 +4,7 @@ module Importer class Order def self.import(user, params) - begin + ActiveRecord::Base.transaction do ensure_country_id_from_params params[:ship_address_attributes] ensure_state_id_from_params params[:ship_address_attributes] ensure_country_id_from_params params[:bill_address_attributes] @@ -45,9 +45,6 @@ def self.import(user, params) end end order.reload - rescue - order.destroy if order && order.persisted? - raise end end @@ -56,7 +53,7 @@ def self.create_shipments_from_params(shipments_hash, order) line_items = order.line_items shipments_hash.each do |s| - shipment = order.shipments.build + shipment = Shipment.new shipment.tracking = s[:tracking] shipment.stock_location = Spree::StockLocation.find_by_admin_name(s[:stock_location]) || Spree::StockLocation.find_by_name!(s[:stock_location]) @@ -64,19 +61,19 @@ def self.create_shipments_from_params(shipments_hash, order) inventory_units.each do |iu| ensure_variant_id_from_params(iu) - unit = shipment.inventory_units.build - unit.order = order + unless line_item = order.line_items.find_by(variant_id: iu[:variant_id]) + line_item = order.contents.add(Spree::Variant.find(iu[:variant_id]), 1) + end # Spree expects a Inventory Unit to always reference a line # item and variant otherwise users might get exceptions when # trying to view these units. Note the Importer might not be # able to find the line item if line_item.variant_id |= iu.variant_id - unit.variant_id = iu[:variant_id] - if line_item = order.line_items.find_by(variant_id: iu[:variant_id]) - unit.line_item = line_item - else - unit.line_item = order.contents.add(Spree::Variant.find(iu[:variant_id]), 1) - end + shipment.inventory_units.new( + order: order, + variant_id: iu[:variant_id], + line_item: line_item + ) end # Mark shipped if it should be. @@ -88,6 +85,7 @@ def self.create_shipments_from_params(shipments_hash, order) end end + order.shipments << shipment shipment.save! shipping_method = Spree::ShippingMethod.find_by_name(s[:shipping_method]) || Spree::ShippingMethod.find_by_admin_name!(s[:shipping_method])