From 71dc3a9d287fd4408e5d5c43cca9347b14dde8ec Mon Sep 17 00:00:00 2001 From: Kanwal Date: Wed, 19 Nov 2014 19:04:16 +0530 Subject: [PATCH 1/2] Custom error for product exclusion rule --- promo/app/controllers/spree/store_controller_decorator.rb | 5 ++++- promo/config/locales/en.yml | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/promo/app/controllers/spree/store_controller_decorator.rb b/promo/app/controllers/spree/store_controller_decorator.rb index aabe1c233e3..4edaf2e2c3b 100644 --- a/promo/app/controllers/spree/store_controller_decorator.rb +++ b/promo/app/controllers/spree/store_controller_decorator.rb @@ -26,14 +26,17 @@ def apply_coupon_code previous_promo = @order.adjustments.promotion.eligible.first fire_event(event_name, :coupon_code => @order.coupon_code) + exclusion_rule = promotion.rules.select{|rule| rule.kind_of? Spree::Promotion::Rules::ProductExclusion}.first promo = @order.adjustments.promotion.detect { |p| p.originator.promotion.code == @order.coupon_code } - if promo.present? and promo.eligible flash[:success] = t(:coupon_code_applied) true elsif previous_promo.present? and promo.present? flash[:error] = t(:coupon_code_better_exists) false + elsif exclusion_rule.present? and !exclusion_rule.eligible?(@order) + flash["product-exclusion-warning"] = t(:coupon_code_not_for_preorders) + false elsif promo.present? flash[:error] = t(:coupon_code_not_eligible) false diff --git a/promo/config/locales/en.yml b/promo/config/locales/en.yml index 894bfe46e52..e3ce25ad6c6 100644 --- a/promo/config/locales/en.yml +++ b/promo/config/locales/en.yml @@ -22,6 +22,7 @@ en: coupon_code_already_applied: The coupon code has already been applied to this order coupon_code_better_exists: The previously applied coupon code results in a better deal coupon_code_not_found: The coupon code you entered doesn't exist. Please try again. + coupon_code_not_for_preorders: You can't use a free shipping coupon on an order that contains one of the new pre-order items. Please place a separate order for the pre-order items. coupon_code_max_usage: Coupon code usage limit exceeded coupon_code_not_eligible: This coupon code is not eligible for this order editing_promotion: Editing Promotion From 0eccbb2383cc199c7c032e8b578fc9d8388cc8d7 Mon Sep 17 00:00:00 2001 From: Kanwal Date: Wed, 19 Nov 2014 22:18:27 +0530 Subject: [PATCH 2/2] Differet messages for shipping coupon error and product exclusion error Checking in the missing file and optimizing the code --- .../controllers/spree/store_controller_decorator.rb | 10 ++++++++-- promo/config/locales/en.yml | 7 ++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/promo/app/controllers/spree/store_controller_decorator.rb b/promo/app/controllers/spree/store_controller_decorator.rb index 4edaf2e2c3b..9a32c3e6561 100644 --- a/promo/app/controllers/spree/store_controller_decorator.rb +++ b/promo/app/controllers/spree/store_controller_decorator.rb @@ -27,6 +27,7 @@ def apply_coupon_code previous_promo = @order.adjustments.promotion.eligible.first fire_event(event_name, :coupon_code => @order.coupon_code) exclusion_rule = promotion.rules.select{|rule| rule.kind_of? Spree::Promotion::Rules::ProductExclusion}.first + exclusion_eligible_products = exclusion_rule.eligible_products.map(&:name) if exclusion_rule.present? promo = @order.adjustments.promotion.detect { |p| p.originator.promotion.code == @order.coupon_code } if promo.present? and promo.eligible flash[:success] = t(:coupon_code_applied) @@ -34,8 +35,13 @@ def apply_coupon_code elsif previous_promo.present? and promo.present? flash[:error] = t(:coupon_code_better_exists) false - elsif exclusion_rule.present? and !exclusion_rule.eligible?(@order) - flash["product-exclusion-warning"] = t(:coupon_code_not_for_preorders) + elsif (exclusion_rule.present? and !exclusion_rule.eligible?(@order)) + items = exclusion_eligible_products && @order.products.map(&:name) + if promo.id == 89 + flash["product-exclusion-preorder-warning"] = "#{t(:coupon_code_not_for_preorders)}, #{t(:go_back_to)} #{t(:shopping_cart)} #{t(:remove_items)}: #{items.join(', ')}".html_safe + else + flash["product-exclusion-warning"] = "#{t(:product_exclusion_rule_warning)} #{t(:listed_items)}: #{items.join(', ')}" + end false elsif promo.present? flash[:error] = t(:coupon_code_not_eligible) diff --git a/promo/config/locales/en.yml b/promo/config/locales/en.yml index e3ce25ad6c6..6a2b0e946ca 100644 --- a/promo/config/locales/en.yml +++ b/promo/config/locales/en.yml @@ -22,11 +22,16 @@ en: coupon_code_already_applied: The coupon code has already been applied to this order coupon_code_better_exists: The previously applied coupon code results in a better deal coupon_code_not_found: The coupon code you entered doesn't exist. Please try again. - coupon_code_not_for_preorders: You can't use a free shipping coupon on an order that contains one of the new pre-order items. Please place a separate order for the pre-order items. + coupon_code_not_for_preorders: Oops! The Free Shipping coupon doesn't apply to the new pre-order items in your cart. You will need to place a separate order for the pre-order items. To get the Free Shipping discount on this order coupon_code_max_usage: Coupon code usage limit exceeded coupon_code_not_eligible: This coupon code is not eligible for this order + product_exclusion_rule_warning: Oops! Your cart contains item(s) that will not work with this promotion. + listed_items: The listed item(s) must be removed before the discount can apply editing_promotion: Editing Promotion current_promotion_usage: 'Current Usage: %{count}' + go_back_to: please go back to the + remove_items: and remove the following items + shopping_cart: Shopping Cart events: spree: checkout: