DanielFischer.com
2007-Mar-31 19:41 UTC
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.
[code=ruby] def discount
coupon = Coupon.find(params[:code])
if coupon.active?
total = total * coupon / 100
else
flash[:notice] = "Invalid Coupon"
end
end[/code]
How would I even run this? Could I do it through the form? <%text_field
:coupon, :discount %>
Thanks for the help,
Daniel
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Wai Tsang
2007-Apr-01 01:27 UTC
Re: Adding a ''coupon-like'' feature to a checkout process?
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:
<label for="coupon">Coupon:</label>
<%= text_field_tag :coupon %>
<%= observe_field(:coupon,
:frequency => 2,
:update => :result,
:url => { :action => :validate },
:with =>
"''coupon=''+escape(value))+''&total=''+
$F(''total'')") %>
<div id="result"><%= render :partial =>
''total'' %></div>
And a partial rhtml (_total.rhtml):
<p>Total is: <%= @total %></p>
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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
DanielFischer.com
2007-Apr-01 16:51 UTC
Re: Adding a ''coupon-like'' feature to a checkout process?
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 Tsang <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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: > > <label for="coupon">Coupon:</label> > <%= text_field_tag :coupon %> > <%= observe_field(:coupon, > :frequency => 2, > :update => :result, > :url => { :action => :validate }, > :with => "''coupon=''+escape(value))+''&total=''+ > $F(''total'')") %> > <div id="result"><%= render :partial => ''total'' %></div> > > And a partial rhtml (_total.rhtml): > <p>Total is: <%= @total %></p> > > 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+Param... > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Wai Tsang
2007-Apr-02 00:01 UTC
Re: Adding a ''coupon-like'' feature to a checkout process?
Daniel Fischer 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 Tsang <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
DanielFischer.com
2007-Apr-03 17:38 UTC
Re: Adding a ''coupon-like'' feature to a checkout process?
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? On Apr 1, 5:01 pm, Wai Tsang <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Daniel Fischer 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 Tsang <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > 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. > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Wai Tsang
2007-Apr-03 20:23 UTC
Re: Adding a ''coupon-like'' feature to a checkout process?
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---