From 26b5250f2c4cdc7874bcb0ee36a3b4478ef3d849 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sat, 22 Dec 2018 23:27:27 +0000 Subject: [PATCH 1/4] Fix line item destroy by forcing quantity to zero and update_inventory before_destroy so that the variant is restocked --- app/models/spree/line_item_decorator.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index 7758ed1d31..2cb82c5761 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -17,6 +17,7 @@ Spree::LineItem.class_eval do before_save :calculate_final_weight_volume, if: :quantity_changed?, unless: :final_weight_volume_changed? after_save :update_units + before_destroy :make_quantity_zero, :update_inventory delegate :unit_description, to: :variant @@ -134,6 +135,12 @@ Spree::LineItem.class_eval do end alias_method_chain :update_inventory, :scoping + # This is necessary before destroying the line item + # so that update_inventory will restore stock to the variant + def make_quantity_zero + self.quantity = 0 + end + def calculate_final_weight_volume if final_weight_volume.present? && quantity_was > 0 self.final_weight_volume = final_weight_volume * quantity / quantity_was From b83ec8d3578ecabf304ec24b9b149d63f947ffe1 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sun, 23 Dec 2018 00:23:49 +0000 Subject: [PATCH 2/4] Add order.shipments reload to line_item.update_inventory. This is necessary when destroying line items as update_inventory may delete the shipment from the order --- app/models/spree/line_item_decorator.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index 2cb82c5761..7f17d57a7a 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -132,6 +132,10 @@ Spree::LineItem.class_eval do def update_inventory_with_scoping scoper.scope(variant) update_inventory_without_scoping + + # This is required because update_inventory may delete the last shipment in the order + # and that makes update_order fail if we don't reload the shipments relation here + order.shipments.reload end alias_method_chain :update_inventory, :scoping From 429e513a775c696768fd52b72678dc1f0626e367 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sun, 23 Dec 2018 09:08:44 +0000 Subject: [PATCH 3/4] Improve line_item code by moving order.shipment reload to a line_item after_destroy hook --- app/models/spree/line_item_decorator.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index 7f17d57a7a..b747fd0055 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -17,7 +17,9 @@ Spree::LineItem.class_eval do before_save :calculate_final_weight_volume, if: :quantity_changed?, unless: :final_weight_volume_changed? after_save :update_units + before_destroy :make_quantity_zero, :update_inventory + after_destroy :reload_order_shipments, :update_order delegate :unit_description, to: :variant @@ -132,10 +134,6 @@ Spree::LineItem.class_eval do def update_inventory_with_scoping scoper.scope(variant) update_inventory_without_scoping - - # This is required because update_inventory may delete the last shipment in the order - # and that makes update_order fail if we don't reload the shipments relation here - order.shipments.reload end alias_method_chain :update_inventory, :scoping @@ -145,6 +143,13 @@ Spree::LineItem.class_eval do self.quantity = 0 end + # TThis is necessary after destroying the line item + # because update_inventory may delete the last shipment in the order + # and that makes update_order fail if we don't reload the shipments + def reload_order_shipments + order.shipments.reload + end + def calculate_final_weight_volume if final_weight_volume.present? && quantity_was > 0 self.final_weight_volume = final_weight_volume * quantity / quantity_was From d44c4778e190304136e831a3bee255916e3f76f0 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sun, 23 Dec 2018 09:22:45 +0000 Subject: [PATCH 4/4] Improve line item code by merging multiple destroy hooks into a single hook: update_inventory_on_destroy. This makes the code easier to read --- app/models/spree/line_item_decorator.rb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index b747fd0055..31b4945c51 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -18,8 +18,7 @@ Spree::LineItem.class_eval do before_save :calculate_final_weight_volume, if: :quantity_changed?, unless: :final_weight_volume_changed? after_save :update_units - before_destroy :make_quantity_zero, :update_inventory - after_destroy :reload_order_shipments, :update_order + before_destroy :update_inventory_before_destroy delegate :unit_description, to: :variant @@ -137,16 +136,16 @@ Spree::LineItem.class_eval do end alias_method_chain :update_inventory, :scoping - # This is necessary before destroying the line item - # so that update_inventory will restore stock to the variant - def make_quantity_zero + def update_inventory_before_destroy + # This is necessary before destroying the line item + # so that update_inventory will restore stock to the variant self.quantity = 0 - end - # TThis is necessary after destroying the line item - # because update_inventory may delete the last shipment in the order - # and that makes update_order fail if we don't reload the shipments - def reload_order_shipments + update_inventory + + # This is necessary after updating inventory + # because update_inventory may delete the last shipment in the order + # and that makes update_order fail if we don't reload the shipments order.shipments.reload end