samedi 9 mai 2015

How to automatically validate coupon and adjust price?

I'm trying to add a coupon to my checkout page, using an AJAX request that validates the coupon and updates the price accordingly. However, on loading the checkout view I get the error message wrong number of arguments (4 for 1..2) referring to the line <%= form_for @actioncode, method="post" ... in the form where the coupon is entered. I've tried to follow the steps here. How should I adjust my code?

The checkout view contains the following form:

<%= form_for @actioncode, method="post", url="check_actioncode_path", remote: true do |f| %>
  <%= f.text_field :coupon_code, :placeholder => "Enter your coupon" %>
  <%= f.submit "Submit Coupon Code" %>
<% end %>

Routes:

post 'check_actioncode' => 'actioncodes#check_actioncode'

In actioncodes controller I have:

def check_actioncode
  @actioncode = Actioncode.find(params[:actioncode])
  respond_to do |format|
    if !@actioncode.nil?
      format.js   {}
    else
      flash.now[:success] = "Action code not found or expired"
    end
  end
end

I'm still trying to build the javascript itself. I try to adjust code from http://ift.tt/1InTN92 but am unsure how to do so.

<script language = "javascript">
<!--
function validate(coupon_code) {
// Somehow check if coupon_code matches one of the actioncodes in the Actioncode model (has a column 'actioncode')
if (...)
 {
 window.alert("Action Code Accepted! Click the Buy Now button to finalize the payment");
 }
// If no match found.
if (...)
 {
 window.alert("Sorry, The Action Code you entered is invalid. Please check and try again!");
 }
}

function CalculateOrder(form) {
 // Will have to change the code below to apply the discount: each actioncode in the Actioncode model has a value in the the column 'discount' (a percentage discount).
 if (form.coupon_code.value == "GOLD20")
 {
 form.discount_rate.value = "...";
 form.discount_rate2.value = "...";
 form.on3.value = "Coupon Entered";
 form.os3.value = "...% Discount Applied";
 }
}
//-->
</script>

Aucun commentaire:

Enregistrer un commentaire