这是购物车。我正在尝试使用促销代码对订单的总价格应用折扣。
我现在想关注这3个模型: 订单 2. promo_code 3. promo_rules
创建订单时,也会创建空的促销代码。与任何其他购物车一样,用户可以在结账时输入促销代码。
订购模式:
var listItems = $("#options li");
listItems.each(function(idx, li) {
var item = $(li);
if (item.scrollWidth > $('#options').innerWidth()) {
//If true, it means text does not fit in the container
//Apply your class or css here
}
});
另外,我正在使用“规则代码”和“金额”字段创建促销规则。
promo_rule.rb
def create_promo_code
PromoCode.create(order_id: self.id)
end
目标是当用户输入促销代码时,将其与数据库中所有促销规则记录的“规则代码字段”进行比较,如果它与1匹配,则该促销规则与促销代码相关联。然后促销代码中的“apply_promo”布尔值更新为true。
请参阅此处的update_prom_code
class PromoRule < ActiveRecord::Base
has_many :promo_codes
end
在订单模型中:
class PromoCode < ActiveRecord::Base
belongs_to :order
belongs_to :promo_rule
after_update :update_promo_code
def update_promo_code
if self.promo_code.present?
@promo_rule = PromoRule.where("rule_code = ?", self.promo_code).take
self.promo_rule_id = @promo_rule
self.apply_promo = true
end
end
end
在这里,我正在尝试检查促销代码是否存在且apply_promo是否为真,然后订单促销金额将使用与促销代码和订单相关联的促销规则金额进行更新。
换句话说,一旦促销代码与促销规则相关联,促销规则“金额”就会从订单总额中扣除。有意义吗?
也在订单模型中:
def apply_promo_code
if self.promo_code.present? && self.promo_code.apply_promo?
self.promo = self.promo_rule.amount
end
end
什么不起作用 promo_code模型中的update_promo_code
我希望我能清楚地解释我的目标。我知道这有点令人困惑。
以下是模型:
promo_code.rb
def total
if self.promo == nil
self[:total] = subtotal + shipping + tax
else
self[:total] = subtotal + shipping - promo + tax
end
end
order.rb
class PromoCode < ActiveRecord::Base
belongs_to :order
belongs_to :promo_rule
after_update :update_promo_code
def update_promo_code
if self.promo_code.present?
@promo_rule = PromoRule.where("rule_code = ?", self.promo_code).take
self.promo_rule_id = @promo_rule
self.apply_promo = true
end
end
end
promo_rule.rb
class Order < ActiveRecord::Base
belongs_to :order_status
belongs_to :user
has_many :order_items
has_many :products, through: :order_items
has_one :shipping_address, through: :user
has_one :purchase
has_one :promo_code
has_one :promo_rule, through: :promo_code
before_create :set_order_status
before_create :set_order_user
before_save :update_subtotal
before_save :update_total
before_update :update_subtotal
before_update :update_total
before_save :tax
before_save :shipping
after_create :create_promo_code
before_update :apply_promo_code
def apply_promo_code
if self.promo_code.present? && self.promo_code.apply_promo?
self.promo = self.promo_rule.amount
end
end
def update_order_promo
self.touch
end
def create_promo_code
PromoCode.create(order_id: self.id)
end
def subtotal
order_items.collect { |oi| oi.valid? ? (oi.quantity * oi.unit_price) : 0 }.sum
end
def tax
shipping_subtotal = shipping + subtotal
self.tax = shipping_subtotal * 0.07
end
def shipping
self.shipping = 5
end
def total
if self.promo == nil
self[:total] = subtotal + shipping + tax
else
self[:total] = subtotal + shipping - promo + tax
end
end
private
def set_shipping_address
end
def set_order_status
self.order_status_id = 1
end
def set_order_user
self.user_id = user_id
end
def update_subtotal
self[:subtotal] = subtotal
end
def update_total
self[:total] = total
end
end
如果您需要了解更多信息,请与我们联系,我会更新问题。
答案 0 :(得分:0)
为了让它以我想要的方式工作,我不得不改变逻辑。所以我将update_promo_code方法移动到了PromoCode模型。我认为这样更干净。
以下是更新后的模型:
<强> promo_code.rb 强>
class PromoCode < ActiveRecord::Base
belongs_to :order
belongs_to :promo_rule
before_update :update_promo_code
def update_promo_code
if PromoRule.where(["rule_code = ?", self.promo_code]).first
@promo_rule = PromoRule.where(["rule_code = ?", self.promo_code]).take
else
@promo_rule = PromoRule.find(2)
end
if self.promo_code == @promo_rule.rule_code
self.promo_rule = @promo_rule
self.apply_promo = true
self.order.update_attribute(:promo, @promo_rule.amount)
else
self.promo_rule = @promo_rule
self.apply_promo = false
true
self.order.update_attribute(:promo, @promo_rule.amount)
end
end
end
<强> order.rb 强>
class Order < ActiveRecord::Base
belongs_to :order_status
belongs_to :user
has_many :order_items
has_many :products, through: :order_items
has_one :shipping_address, through: :user
has_one :purchase
has_one :promo_code
has_one :promo_rule, through: :promo_code
before_create :set_order_status
before_create :set_order_user
before_save :update_subtotal
before_save :update_total
before_update :update_subtotal
before_update :update_total
before_save :tax
before_save :shipping
after_create :create_promo_code
def create_promo_code
PromoCode.create(order_id: self.id, apply_promo: false)
end
def subtotal
order_items.collect { |oi| oi.valid? ? (oi.quantity * oi.unit_price) : 0 }.sum
end
def tax
shipping_subtotal = shipping + subtotal
self.tax = shipping_subtotal * 0.07
end
def shipping
self.shipping = 5
end
def total
if self.promo == nil
self[:total] = subtotal + shipping + tax
else
self[:total] = subtotal + shipping - promo + tax
end
end
private
def set_shipping_address
end
def set_order_status
self.order_status_id = 1
end
def set_order_user
self.user_id = user_id
end
def update_subtotal
self[:subtotal] = subtotal
end
def update_total
self[:total] = total
end
end