From 26b5250f2c4cdc7874bcb0ee36a3b4478ef3d849 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Sat, 22 Dec 2018 23:27:27 +0000 Subject: [PATCH] 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