Skip to content

Instantly share code, notes, and snippets.

@ronzalo
Created September 6, 2016 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronzalo/0d877ba89116a3e93cbd5a50535a6eae to your computer and use it in GitHub Desktop.
Save ronzalo/0d877ba89116a3e93cbd5a50535a6eae to your computer and use it in GitHub Desktop.
Coupon input in Spree cart
<!-- app/views/spree/orders/_form.html.erb -->
<%= order_form.text_field :coupon_code, :class => 'form-control', placeholder: Spree.t(:coupon_code) %>
# app/controllers/spree/orders_controller_decorator.rb
module Spree
OrdersController.class_eval do
before_action :apply_coupon_code
private
def apply_coupon_code
if params[:order] && params[:order][:coupon_code]
@order.coupon_code = params[:order][:coupon_code]
handler = PromotionHandler::Coupon.new(@order).apply
if handler.error.present?
flash.now[:error] = handler.error
respond_with(@order) { |format| format.html { render :edit } } && return
elsif handler.success
flash[:success] = handler.success
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment