简体   繁体   中英

How can I put PayPal discount codes on my checkout

Just doing a simple code to apply promo code on my PayPal button. I just did like below but the validate() function did not alert.

I hope all codes are correct. Please check the Fiddle and do let me know why the functions are not working.

 function validate( text1, text2 ) { if ( text1 == text2 ) { window.alert( "Nice, you get a 10% Discount!" ); } if ( text1 !== text2 ) { window.alert( "Sorry, No Discount!" ); } } function CalculateOrder( form ) { if ( form.text1.value == "GOLD10" ) { form.discount_rate.value = "10"; form.discount_rate2.value = "10"; form.on3.value = "Coupon Entered"; form.os3.value = "GOLD10"; } } 
 <form target="_self" action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_cart"/> <input type="hidden" name="add" value="1"/> <input type="hidden" name="business" value="fanciedmedia@gmail.com"/> <input type="hidden" name="item_name" value="Blue Kidsport Burst"/> <input type="hidden" name="amount" value="149.00"/> <input type="hidden" name="currency_code" value="USD"/> <input type="hidden" name="no_shipping" value="2"> <input type="hidden" name="weight_unit" value="lbs"> <input type="hidden" name="baseamt" value="149.00"/> <input type="hidden" name="item_number" value="BUONBL"> <span id="dist"></span> <input type="text" name="text1"> <input type="button" value="Check It" name="Submit" onclick=javascript&colon;validate(text1.value, "GOLD10")> <br> <input name="submit" type="image" alt="Add to Cart" src="add_to.png" onclick=CalculateOrder(this.form)/> </form> 

You missed adding simple quotes ' in the onclick attributes, and you should refer to form object in order to get text1 value:

onclick=javascript&colon;validate(text1.value, "GOLD10")
        ^^                        ^^^                   ^^

onclick=CalculateOrder(this.form)
        ^^                       ^^

It should be:

onclick='javascript&colon;validate(form.text1.value, "GOLD10")'
onclick='CalculateOrder(this.form)'

Try this code:

 function validate( text1, text2 ) { if ( text1 == text2 ) { window.alert( "Nice, you get a 10% Discount!" ); } if ( text1 !== text2 ) { window.alert( "Sorry, No Discount!" ); } } function CalculateOrder( form ) { if ( form.text1.value == "GOLD10" ) { form.discount_rate.value = "10"; form.discount_rate2.value = "10"; form.on3.value = "Coupon Entered"; form.os3.value = "GOLD10"; } alert('submitting form'); form.submit(); } 
 <form target="_self" action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_cart"/> <input type="hidden" name="add" value="1"/> <input type="hidden" name="business" value="fanciedmedia@gmail.com"/> <input type="hidden" name="item_name" value="Blue Kidsport Burst"/> <input type="hidden" name="amount" value="149.00"/> <input type="hidden" name="currency_code" value="USD"/> <input type="hidden" name="no_shipping" value="2"> <input type="hidden" name="weight_unit" value="lbs"> <input type="hidden" name="baseamt" value="149.00"/> <input type="hidden" name="item_number" value="BUONBL"> <input type="hidden" name="discount_rate" value=""> <input type="hidden" name="discount_rate2" value=""> <input type="hidden" name="on3" value=""> <input type="hidden" name="os3" value=""> <span id="dist"></span> <input type="text" name="text1"> <input type="button" value="Check It" name="Submit" onclick='javascript&colon;validate(form.text1.value, "GOLD10")'> <br> <input type="button" alt="Add to Cart" src="add_to.png" value="add to cart" onclick='CalculateOrder(form)'/> </form> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM