Class: Spree::OrderMerger

Inherits:
Object
  • Object
show all
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)

Instance Method Summary (collapse)

Constructor Details

- (OrderMerger) initialize(order)

Create the OrderMerger

into.

Parameters:

  • order (Spree::Order)

    The order which line items will be merged



21
22
23
# File 'app/models/spree/order_merger.rb', line 21

def initialize(order)
  @order = order
end

Instance Attribute Details

- (Spree::Order) order

Returns The order which items wll be merged into.

Returns:

  • (Spree::Order)

    The order which items wll be merged into.



12
13
14
# File 'app/models/spree/order_merger.rb', line 12

def order
  @order
end

Instance Method Details

- (void) merge!(other_order, user = nil)

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.

Examples:

initial_order = Spree::Order.find(1)
order_to_merge = Spree::Order.find(2)
merger = Spree::OrderMerger.new(initial_order)
merger.merge!(order_to_merge)
# order_to_merge is destroyed, initial order now contains the line items
# of order_to_merge

Parameters:

  • other_order (Spree::Order)

    An order which will be merged in to the

  • user (Spree::User) (defaults to: nil)

    Associate the order the user specified. If not



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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)
  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