Skip to content

Instantly share code, notes, and snippets.

@ricardokrieg
Created October 17, 2013 00:13
Show Gist options
  • Save ricardokrieg/7017224 to your computer and use it in GitHub Desktop.
Save ricardokrieg/7017224 to your computer and use it in GitHub Desktop.
Dont allow more than one promotional item in a Spree Order
add_column :spree_products, :promotional, :boolean
Spree::OrderPopulator.class_eval do
def attempt_cart_add(variant_id, quantity)
quantity = quantity.to_i
# 2,147,483,647 is crazy.
# See issue #2695.
if quantity > 2_147_483_647
errors.add(:base, Spree.t(:please_enter_reasonable_quantity, :scope => :order_populator))
return false
end
variant = Spree::Variant.find(variant_id)
if quantity > 0
### CUSTOM CODE - BEGIN
if variant.promotional? and @order.line_items.any? {|line_item| line_item.variant.promotional?}
errors.add(:base, :promotional_error_message)
return false
end
### CUSTOM CODE - END
line_item = @order.contents.add(variant, quantity, currency)
unless line_item.valid?
errors.add(:base, line_item.errors.messages.values.join(" "))
return false
end
end
end
end
Spree::Variant.class_eval do
def promotional?
variant.product.promotional?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment