Class: Spree::PromotionHandler::Cart

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/promotion_handler/cart.rb

Overview

Decides which promotion should be activated given the current order context

By activated it doesn't necessarily mean that the order will have a discount for every activated promotion. It means that the discount will be created and might eventually become eligible. The intention here is to reduce overhead. e.g. a promotion that requires item A to be eligible shouldn't be eligible unless item A is added to the order.

It can be used as a wrapper for custom handlers as well. Different applications might have completely different requirements to make the promotions system accurate and performant. Here they can plug custom handler to activate promos as they wish once an item is added to cart

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Cart) initialize(order, line_item = nil)

Returns a new instance of Cart



19
20
21
# File 'app/models/spree/promotion_handler/cart.rb', line 19

def initialize(order, line_item = nil)
  @order, @line_item = order, line_item
end

Instance Attribute Details

- (Object) error

Returns the value of attribute error



17
18
19
# File 'app/models/spree/promotion_handler/cart.rb', line 17

def error
  @error
end

- (Object) line_item (readonly)

Returns the value of attribute line_item



16
17
18
# File 'app/models/spree/promotion_handler/cart.rb', line 16

def line_item
  @line_item
end

- (Object) order (readonly)

Returns the value of attribute order



16
17
18
# File 'app/models/spree/promotion_handler/cart.rb', line 16

def order
  @order
end

- (Object) success

Returns the value of attribute success



17
18
19
# File 'app/models/spree/promotion_handler/cart.rb', line 17

def success
  @success
end

Instance Method Details

- (Object) activate



23
24
25
26
27
28
29
# File 'app/models/spree/promotion_handler/cart.rb', line 23

def activate
  promotions.each do |promotion|
    if (line_item && promotion.eligible?(line_item, promotion_code: promotion_code(promotion))) || promotion.eligible?(order, promotion_code: promotion_code(promotion))
      promotion.activate(line_item: line_item, order: order, promotion_code: promotion_code(promotion))
    end
  end
end