Class: Workarea::Pricing::Discount::FreeGift
- Inherits:
-
Workarea::Pricing::Discount
- Object
- Workarea::Pricing::Discount
- Workarea::Pricing::Discount::FreeGift
- Defined in:
- app/models/workarea/pricing/discount/free_gift.rb
Overview
This discount allows free gifts, which are automatically added to the Order when it qualifies.
Constant Summary
Constants included from Conditions::OrderTotal
Conditions::OrderTotal::OPERATORS
Instance Attribute Summary collapse
-
#category_ids ⇒ String
Get the free gifts when purchasing from one of these Catalog::Category.
-
#product_ids ⇒ String
Get the free gift when purchasing one of Catalog::Product.
-
#sku ⇒ String
The SKU of the free gift.
Attributes inherited from Workarea::Pricing::Discount
#allow_sale_items, #auto_deactivated, #auto_deactivated_at, #compatible_discount_ids, #excluded_category_ids, #excluded_product_ids, #name, #price_level, #redemptions, #single_use
Class Method Summary collapse
Instance Method Summary collapse
-
#apply(order) ⇒ Object
Apply the discount, which adds the free gift item to the order, along with a dummy price adjustment.
-
#catalog_qualifies?(order) ⇒ Boolean
Qualifier method for whether either products or categories match so this order receives the discount.
-
#order_not_empty?(order) ⇒ Boolean
Qualifier method for whether the order is empty.
-
#remove_from(order, shipping = nil) ⇒ Workarea::Order
Remove any free items this discount may have added to the order.
Methods included from Conditions::UserTags
#user_tag?, #user_tags_qualify?
Methods included from Conditions::PromoCodes
#generated_codes, #promo_code?, #promo_codes_qualify?, #valid?
Methods included from Conditions::OrderTotal
#order_total?, #order_total_qualifies?
Methods inherited from Workarea::Pricing::Discount
#<=>, add_qualifier, auto_deactivate, #auto_deactivate!, #auto_deactivated?, #auto_deactivates_at, #can_be_used_by?, #compatible_discounts, #compatible_with?, #excludes_category_id?, #excludes_product_id?, #has_been_redeemed?, #last_redemption, #log_redemption, qualifier_methods, #qualifies?
Methods included from Commentable
#add_subscription, #remove_subscription
Methods included from Releasable
#changesets_with_children, #destroy, #in_release, #release_changes, #release_originals, #save_changeset, #skip_changeset, #without_release
Methods included from Segmentable
#active?, #active_segment_ids_with_children, #segmented?, #segments
Methods included from Release::Activation
#activate_with?, #create_activation_changeset, #save_activate_with, #was_active?
Methods included from Mongoid::Document::Taggable
Methods included from ApplicationDocument
Methods included from Sidekiq::Callbacks
assert_valid_config!, async, disable, enable, inline, #run_callbacks
Methods included from Mongoid::Document
Instance Attribute Details
#category_ids ⇒ String
Returns get the free gifts when purchasing from one of these Catalog::Category.
28 |
# File 'app/models/workarea/pricing/discount/free_gift.rb', line 28 field :category_ids, type: Array, default: [] |
#product_ids ⇒ String
Returns get the free gift when purchasing one of Catalog::Product.
21 |
# File 'app/models/workarea/pricing/discount/free_gift.rb', line 21 field :product_ids, type: Array, default: [] |
Class Method Details
.model_name ⇒ Object
36 37 38 |
# File 'app/models/workarea/pricing/discount/free_gift.rb', line 36 def self.model_name Discount.model_name end |
Instance Method Details
#apply(order) ⇒ Object
Apply the discount, which adds the free gift item to the order, along with a dummy price adjustment.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/models/workarea/pricing/discount/free_gift.rb', line 74 def apply(order) free_item = Workarea::Order::Item.new( { sku: sku, free_gift: true, quantity: 1 }.merge(free_product_attributes(sku)) ) sell_price = Sku.find_or_create_by(id: sku).sell_price free_item.adjust_pricing(adjustment_data(sell_price, 1)) order.add_item(free_item) end |
#catalog_qualifies?(order) ⇒ Boolean
Qualifier method for whether either products or categories match so this order receives the discount.
64 65 66 67 |
# File 'app/models/workarea/pricing/discount/free_gift.rb', line 64 def catalog_qualifies?(order) return true if product_ids.blank? && category_ids.blank? products_qualify?(order) || categories_qualify?(order) end |
#order_not_empty?(order) ⇒ Boolean
Qualifier method for whether the order is empty. You can’t receive a free gift on an empty order.
54 55 56 |
# File 'app/models/workarea/pricing/discount/free_gift.rb', line 54 def order_not_empty?(order) order.items.reject(&:free_gift?).any? end |
#remove_from(order, shipping = nil) ⇒ Workarea::Order
Remove any free items this discount may have added to the order.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/models/workarea/pricing/discount/free_gift.rb', line 95 def remove_from(order, shipping = nil) order.items.each do |item| matches = !!item.price_adjustments.detect do |adjustment| adjustment.data['discount_id'] == id.to_s end order.remove_item(item) if matches end order end |