Module: Spree::Order::GiftCard

Extended by:
ActiveSupport::Concern
Included in:
Spree::Order
Defined in:
app/models/spree/order/gift_card.rb

Instance Method Summary collapse

Instance Method Details

#apply_gift_card(gift_card) ⇒ Spree::Order

Applies a gift card to the order

Parameters:

Returns:



27
28
29
# File 'app/models/spree/order/gift_card.rb', line 27

def apply_gift_card(gift_card)
  Spree::Dependencies.gift_card_apply_service.constantize.call(gift_card: gift_card, order: self)
end

#gift_card_totalDecimal

Returns the total amount of the gift card applied to the order

Returns:

  • (Decimal)


15
16
17
18
19
20
21
22
# File 'app/models/spree/order/gift_card.rb', line 15

def gift_card_total
  return 0.to_d unless gift_card.present?

  store_credit_ids = payments.store_credits.valid.pluck(:source_id)
  store_credits = Spree::StoreCredit.where(id: store_credit_ids, originator: gift_card)

  store_credits.sum(:amount)
end

#recalculate_gift_cardObject



37
38
39
40
41
42
# File 'app/models/spree/order/gift_card.rb', line 37

def recalculate_gift_card
  applied_gift_card = gift_card

  remove_gift_card
  apply_gift_card(applied_gift_card)
end

#redeem_gift_cardObject



44
45
46
47
48
# File 'app/models/spree/order/gift_card.rb', line 44

def redeem_gift_card
  return unless gift_card.present?

  Spree::Dependencies.gift_card_redeem_service.constantize.call(gift_card: gift_card)
end

#remove_gift_cardSpree::Order

Removes a gift card from the order

Returns:



33
34
35
# File 'app/models/spree/order/gift_card.rb', line 33

def remove_gift_card
  Spree::Dependencies.gift_card_remove_service.constantize.call(order: self)
end