Class: Workarea::Pricing::Discount::Product

Inherits:
Workarea::Pricing::Discount show all
Includes:
Conditions::ItemQuantity, Conditions::OrderTotal, Conditions::PromoCodes, Conditions::UserTags, FlatOrPercentOff
Defined in:
app/models/workarea/pricing/discount/product.rb

Constant Summary

Constants included from Conditions::OrderTotal

Conditions::OrderTotal::OPERATORS

Constants included from FlatOrPercentOff

FlatOrPercentOff::AMOUNT_TYPES

Instance Attribute Summary collapse

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

Methods included from Conditions::UserTags

#user_tag?, #user_tags_qualify?

Methods included from Conditions::ItemQuantity

#item_quantity?, #items_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 included from FlatOrPercentOff

#amount_calculator

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?, #remove_from

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

included

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

assert_valid_config!, async, disable, enable, inline, #run_callbacks

Methods included from Mongoid::Document

#embedded_children

Instance Attribute Details

#product_idsArray

Returns discount applies to these Catalog::Product ids.

Returns:



16
# File 'app/models/workarea/pricing/discount/product.rb', line 16

field :product_ids, type: Array, default: []

Class Method Details

.model_nameObject



21
22
23
# File 'app/models/workarea/pricing/discount/product.rb', line 21

def self.model_name
  Discount.model_name
end

Instance Method Details

#apply(order) ⇒ Workarea::Order

Creates the price adjustments for the discount on the matching items.

Parameters:

Returns:



47
48
49
50
51
52
53
54
55
56
# File 'app/models/workarea/pricing/discount/product.rb', line 47

def apply(order)
  order.items.each do |item|
    next unless item_qualifies?(item)

    value = amount_calculator.calculate(item.current_unit_price, item.quantity)
    item.adjust_pricing(adjustment_data(value, item.quantity))
  end

  order
end

#item_qualifies?(item) ⇒ Boolean

Qualifier method to check whether any items in this order qualify for this discount.

Parameters:

Returns:

  • (Boolean)


37
38
39
# File 'app/models/workarea/pricing/discount/product.rb', line 37

def item_qualifies?(item)
  super && item.product_id.in?(product_ids)
end