Adding a 'coupon-like' feature to a checkout process?

I am not sure how to go about this. So far I only have my coupon model
built.

Basically what I am trying to achieve is simply being able to ‘use’ a
coupon that is created, to knock the price down on the total of the
model that is handling the checkout.

I was wondering if the ‘model that is handling the checkout’ even
needs to be related to the coupon. I was thinking I could simply do
@coupon = Coupon.new

Another thing is, I am not sure how to set up the form functionality
of being able to update something on the screen. Maybe ajax?

Imagine something like this on the browser:

Buy so and so : $15

Total: $15

(Form to enter billing address)

(coupon field) (update) <— how would this work? Basically have the
coupon if valid, change Total.

Reset / Checkout

I tried creating a method like this in the coupon model but I am
coding pretty blind - so let me know if there is a better way to
tackle this.

def discount coupon = Coupon.find(params[:code]) if coupon.active? total = total * coupon / 100 else flash[:notice] = "Invalid Coupon" end end
How would I even run this? Could I do it through the form? <%=
text_field :coupon, :discount %>

Thanks for the help,
Daniel

Hi Daniel,

If you want to know whether the coupon is valid or not on the screen
(i.e. without any form post), you probably would want to use AJAX to
observe the coupon field.

For example, you can put something like this in the view:

Coupon:
<%= text_field_tag :coupon %>
<%= observe_field(:coupon,
:frequency => 2,
:update => :result,
:url => { :action => :validate },
:with => “‘coupon=’+escape(value))+‘&total=’+
$F(‘total’)”) %>

<%= render :partial => 'total' %>

And a partial rhtml (_total.rhtml):

Total is: <%= @total %>

When of the text field has been changed, the request will be sent to the
controller. Here’s what you may put in the controller:
def validate
@total = params[:total]
code = params[:coupon]
if Coupon.exists?(code)
@total = @total * Coupon.find(code).value / 100
render :partial => ‘total’
else
render :text => ‘invalid coupon’
end
end

See this link for more information about observe_field:
http://wiki.rubyonrails.org/rails/pages/observe_field+-+Passing+Parameters

Wai,

hmm I tried this out - but I am not sure I am doing it right. Mainly
because I am trying to integrate this into the membership/upgrade view
yet the coupon is related to the coupon/validate (controller,action).

I tried to plug it in to that form snippet you gave me, but it doesn’t
work. Any guidance towards this?

On Mar 31, 6:27 pm, Wai T. [email protected]

Wai,

Sorry to bug you again. But I’m not sure I want to do this with ajax
at the moment. Mainly because the project is using jquery as well, so
how would I do this with only forms? Would it have to be two forms in
a view?

It doesn’t have to two forms in a view, it is just more clean and
convenient to use the same partial when the form load, and when the
update has been made. I believe you can use JQuery to render the
partials just like AJAX, but I am not familiar with it.

Daniel F. wrote:

Wai,

hmm I tried this out - but I am not sure I am doing it right. Mainly
because I am trying to integrate this into the membership/upgrade view
yet the coupon is related to the coupon/validate (controller,action).

I tried to plug it in to that form snippet you gave me, but it doesn’t
work. Any guidance towards this?

On Mar 31, 6:27 pm, Wai T. [email protected]

Did you include the protype.js? Beside the typo (the extra closing
blanket) in the observe_field tag, I am assuming you have a text field
with id total. You may try to experiment with the observer_field first
since debugging AJAX calls are somewhat tricky.