Class: Spree::PromotionHandler::Coupon
- Inherits:
-
Object
- Object
- Spree::PromotionHandler::Coupon
- Defined in:
- app/models/spree/promotion_handler/coupon.rb
Instance Attribute Summary collapse
-
#error ⇒ Object
Returns the value of attribute error.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#status_code ⇒ Object
Returns the value of attribute status_code.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
-
#success ⇒ Object
Returns the value of attribute success.
Instance Method Summary collapse
-
#adjustments_amount ⇒ Numeric
Returns the amount of adjustments for the promotion.
- #apply ⇒ Object
-
#initialize(order, options = {}) ⇒ Coupon
constructor
A new instance of Coupon.
-
#promotion ⇒ Spree::Promotion
Returns the promotion for the order.
- #remove(coupon_code) ⇒ Object
- #set_error_code(code, locale_options = {}) ⇒ Object
- #set_success_code(code) ⇒ Object
-
#successful? ⇒ Boolean
Returns true if the code was applied successfully.
Constructor Details
#initialize(order, options = {}) ⇒ Coupon
Returns a new instance of Coupon.
7 8 9 10 11 |
# File 'app/models/spree/promotion_handler/coupon.rb', line 7 def initialize(order, = {}) @order = order @store = order.store = end |
Instance Attribute Details
#error ⇒ Object
Returns the value of attribute error.
5 6 7 |
# File 'app/models/spree/promotion_handler/coupon.rb', line 5 def error @error end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'app/models/spree/promotion_handler/coupon.rb', line 4 def end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
4 5 6 |
# File 'app/models/spree/promotion_handler/coupon.rb', line 4 def order @order end |
#status_code ⇒ Object
Returns the value of attribute status_code.
5 6 7 |
# File 'app/models/spree/promotion_handler/coupon.rb', line 5 def status_code @status_code end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
4 5 6 |
# File 'app/models/spree/promotion_handler/coupon.rb', line 4 def store @store end |
#success ⇒ Object
Returns the value of attribute success.
5 6 7 |
# File 'app/models/spree/promotion_handler/coupon.rb', line 5 def success @success end |
Instance Method Details
#adjustments_amount ⇒ Numeric
Returns the amount of adjustments for the promotion
107 108 109 110 111 112 |
# File 'app/models/spree/promotion_handler/coupon.rb', line 107 def adjustments_amount @adjustments_amount ||= @order.all_adjustments.promotion.eligible. where(source: promotion&.actions). sum(:amount) end |
#apply ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/models/spree/promotion_handler/coupon.rb', line 13 def apply if load_gift_card_code if @gift_card.expired? set_error_code :gift_card_expired return self elsif @gift_card.redeemed? set_error_code :gift_card_already_redeemed return self end result = order.apply_gift_card(@gift_card) if result.success? set_success_code(:gift_card_applied) else set_error_code(result.value, result.error.value || {}) end return self end if order.coupon_code.present? if promotion.present? && promotion.actions.exists? handle_present_promotion elsif store.promotions.with_coupon_code(order.coupon_code).try(:expired?) set_error_code :coupon_code_expired else set_error_code :coupon_code_not_found end else set_error_code :coupon_code_not_found end self end |
#promotion ⇒ Spree::Promotion
Returns the promotion for the order
98 99 100 101 102 |
# File 'app/models/spree/promotion_handler/coupon.rb', line 98 def promotion @promotion ||= store.promotions.active.includes( :promotion_rules, :promotion_actions ).with_coupon_code(order.coupon_code) end |
#remove(coupon_code) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/models/spree/promotion_handler/coupon.rb', line 49 def remove(coupon_code) if order.gift_card result = order.remove_gift_card if result.success? set_success_code(:gift_card_removed) else set_error_code(result.value) end return self end promotion = order.promotions.with_coupon_code(coupon_code) if promotion.present? # Order promotion has to be destroyed before line item removing order.order_promotions.where(promotion_id: promotion.id).destroy_all if promotion.multi_codes? coupon_code = promotion.coupon_codes.find_by(order: order) coupon_code&.remove_from_order else promotion.touch end remove_promotion_adjustments(promotion) remove_promotion_line_items(promotion) order.update_with_updater! set_success_code :adjustments_deleted else set_error_code :coupon_code_not_found end self end |
#set_error_code(code, locale_options = {}) ⇒ Object
90 91 92 93 |
# File 'app/models/spree/promotion_handler/coupon.rb', line 90 def set_error_code(code, = {}) @status_code = code @error = Spree.t(code, ) end |
#set_success_code(code) ⇒ Object
85 86 87 88 |
# File 'app/models/spree/promotion_handler/coupon.rb', line 85 def set_success_code(code) @status_code = code @success = Spree.t(code) end |
#successful? ⇒ Boolean
Returns true if the code was applied successfully
117 118 119 |
# File 'app/models/spree/promotion_handler/coupon.rb', line 117 def successful? success.present? && error.blank? end |