Class: Spree::OrderMerger
- Inherits:
-
Object
- Object
- Spree::OrderMerger
- Defined in:
- app/models/spree/order_merger.rb
Overview
Spree::OrderMerger is responsible for taking two orders and merging them together by adding the line items from additional orders to the order that the OrderMerger is initialized with.
Orders that are merged should be incomplete orders which should belong to the same user. They should also be in the same currency.
Instance Attribute Summary collapse
-
#order ⇒ Spree::Order
The order which items wll be merged into.
Instance Method Summary collapse
-
#initialize(order) ⇒ OrderMerger
constructor
Create the OrderMerger.
-
#merge!(other_order, user = nil) ⇒ void
Merge a second order in to the order the OrderMerger was initialized with.
Constructor Details
#initialize(order) ⇒ OrderMerger
Create the OrderMerger
into.
21 22 23 |
# File 'app/models/spree/order_merger.rb', line 21 def initialize(order) @order = order end |
Instance Attribute Details
#order ⇒ Spree::Order
Returns The order which items wll be merged into.
14 15 16 |
# File 'app/models/spree/order_merger.rb', line 14 def order @order end |
Instance Method Details
#merge!(other_order, user = nil) ⇒ void
This method returns an undefined value.
Merge a second order in to the order the OrderMerger was initialized with
The line items from ‘other_order` will be merged in to the `order` for this OrderMerger object. If the line items are for the same variant, it will add the quantity of the incoming line item to the existing line item. Otherwise, it will assign the line item to the new order.
After the orders have been merged the ‘other_order` will be destroyed.
order the OrderMerger was initialized with. specified, the order user association will not be changed.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/spree/order_merger.rb', line 48 def merge!(other_order, user = nil) if other_order.currency == order.currency other_order.line_items.each do |other_order_line_item| current_line_item = find_matching_line_item(other_order_line_item) handle_merge(current_line_item, other_order_line_item) end end set_user(user) if order.valid? persist_merge # So that the destroy doesn't take out line items which may have been re-assigned other_order.line_items.reload other_order.destroy end end |