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.
This commit is contained in:
Matt-Yorkley
2021-05-11 14:51:33 +01:00
parent cb92231835
commit 0178cd7530
3 changed files with 21 additions and 37 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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)