Created
October 17, 2013 00:13
-
-
Save ricardokrieg/7017224 to your computer and use it in GitHub Desktop.
Dont allow more than one promotional item in a Spree Order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_column :spree_products, :promotional, :boolean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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