From 0178cd753035bf35fa02d40820fc982fb02b5c70 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Tue, 11 May 2021 14:51:33 +0100 Subject: [PATCH] Adapt OrderContents to allow use with frontend cart service and updating :max_quantity The cart service and it's surrounding logic is a bit messy, as it jumps through hoops to add extra logic for the :max_quantity attribute used with the "group buy" feature. This is the last bit of code where an order's line items could be changed where we were not using OrderContents. --- app/models/spree/order.rb | 34 +----------------------------- app/models/spree/order_contents.rb | 16 ++++++++++++++ app/services/cart_service.rb | 8 +++---- 3 files changed, 21 insertions(+), 37 deletions(-) diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index f9f14842c3..94c950abca 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -243,43 +243,11 @@ module Spree return_authorizations.any?(&:authorized?) end - # This is currently used when adding a variant to an order in the BackOffice. - # Spree::OrderContents#add is equivalent but slightly different from add_variant below. + # OrderContents should always be used when modifying an order's line items def contents @contents ||= Spree::OrderContents.new(self) end - # This is currently used when adding a variant to an order in the FrontOffice. - # This add_variant is equivalent but slightly different from Spree::OrderContents#add above. - # Spree::OrderContents#add is the more modern version in Spree history - # but this add_variant has been customized for OFN FrontOffice. - def add_variant(variant, quantity = 1, max_quantity = nil) - line_items.reload - current_item = find_line_item_by_variant(variant) - - # Notify bugsnag if we get line items with a quantity of zero - if quantity == 0 - Bugsnag.notify(RuntimeError.new("Zero Quantity Line Item"), - current_item: current_item.as_json, - line_items: line_items.map(&:id), - variant: variant.as_json) - end - - if current_item - current_item.quantity = quantity - current_item.max_quantity = max_quantity - current_item.save - else - current_item = Spree::LineItem.new(quantity: quantity, max_quantity: max_quantity) - current_item.variant = variant - current_item.price = variant.price - line_items << current_item - end - - reload - current_item - end - # Associates the specified user with the order. def associate_user!(user) self.user = user diff --git a/app/models/spree/order_contents.rb b/app/models/spree/order_contents.rb index c50ac0cd2e..832c1bafd3 100644 --- a/app/models/spree/order_contents.rb +++ b/app/models/spree/order_contents.rb @@ -26,6 +26,22 @@ module Spree line_item end + def update_or_create(variant, attributes) + line_item = find_line_item_by_variant(variant) + + if line_item + line_item.update(attributes) + else + line_item = Spree::LineItem.new(attributes) + line_item.variant = variant + line_item.price = variant.price + order.line_items << line_item + end + + order.reload + line_item + end + def update_cart(params) if order.update_attributes(params) discard_empty_line_items diff --git a/app/services/cart_service.rb b/app/services/cart_service.rb index 77d3bea20e..589a57f22b 100644 --- a/app/services/cart_service.rb +++ b/app/services/cart_service.rb @@ -69,9 +69,9 @@ class CartService end def cart_add(variant, quantity, max_quantity) - final_quantity, final_max_quantity = final_quantities(variant, quantity, max_quantity) - if final_quantity > 0 - @order.add_variant(variant, final_quantity, final_max_quantity) + attributes = final_quantities(variant, quantity, max_quantity) + if attributes[:quantity] > 0 + @order.contents.update_or_create(variant, attributes) else @order.contents.remove(variant) end @@ -84,7 +84,7 @@ class CartService final_quantity = [quantity, on_hand].min final_max_quantity = max_quantity # max_quantity is not capped - [final_quantity, final_max_quantity] + { quantity: final_quantity, max_quantity: final_max_quantity } end def overwrite_variants(variants)