Events Made Easy › Forums › Tips › Coupon Codes
Tagged: coupon discount
- This topic has 19 replies, 4 voices, and was last updated 9 years, 8 months ago by Anonymous.
-
AuthorPosts
-
Tue 20 May 2014 at 04:16 #52201AnonymousInactive
For a site I’m managing, my client wanted event registrants to be able to enter a coupon code to get a discount off the normal price. After reading some other posts in the forum, and tweaking the discount example in the FAQ, I came up with a solution that worked nicely for my needs.
First, I added a custom “Coupon” text field, and inserted this field into the RSVP form. In my case, I inserted it between the Seats and Comment lines:
... <tr><th scope='row'>Seats*:</th><td>#_SPACES</td></tr> <tr><th scope='row'>Coupon:</th><td>#_FIELD{1}</td> <tr><th scope='row'>Comment:</th><td>#_COMMENT</td></tr> ...
The following functions were then added to the theme’s
functions.php
file. Because I wanted my coupon codes to be tied to specific event types, I made event categories, and queried the booked event’s category to match to the proper code.Right now only one category has codes, but I plan on adding more later. I’m also allowing for both upper- and lower-case matches for the coupon codes, though I might change to a regular expression comparison later. (And yes, the codes and categories in the code below were changed for this example.)
/** * Add a hook for the Events Made Easy system to allow for coupon codes */ add_action('eme_insert_rsvp_action', 'my_eme_coupons',20,1); /** * Custom function to calculate coupon code discounts for events */ function my_eme_coupons($booking) { global $wpdb; $bookings_table = $wpdb->prefix.BOOKINGS_TBNAME; $where = array(); $fields = array(); // Grab the coupon code from the extra answers $event_id = $booking['event_id']; $answers = eme_get_answers($event_id); $coupon = $answers[0]['answer']; // Collect the event categories $event_cat = eme_get_event_categories($event_id); // As long as the coupon code isn't empty, look for matches if ($coupon != '') { if ($event_cat[0] == "Category1") { $price=$booking['booking_price']; // If coupon code used, apply the appropriate price change if ($coupon == "XXXXX" || $coupon == "xxxxx") $price = 27.5; $fields['booking_price'] = $price; $where['booking_id'] = $booking['booking_id']; $wpdb->update($bookings_table, $fields, $where); } } return; }
Tue 20 May 2014 at 17:12 #52206FrankyKeymasterThanks for sharing !
Thu 22 May 2014 at 21:13 #52249AnonymousInactiveJust found an error in the code above. Here is the correct code:
/** * Add a hook for the Events Made Easy system to allow for coupon codes */ add_action('eme_insert_rsvp_action', 'my_eme_coupons',20,1); /** * Custom function to calculate coupon code discounts for events */ function my_eme_coupons($booking) { global $wpdb; $bookings_table = $wpdb->prefix.BOOKINGS_TBNAME; $where = array(); $fields = array(); // Grab the coupon code from the extra answers $event_id = $booking['event_id']; $booking_id = $booking['booking_id']; $answers = eme_get_answers($booking_id); $coupon = $answers[0]['answer']; // Collect the event categories $event_cat = eme_get_event_categories($event_id); // As long as the coupon code isn't empty, look for matches if ($coupon != '') { if ($event_cat[0] == "Category1") { $price=$booking['booking_price']; // If coupon code used, apply the appropriate price change if ($coupon == "XXXXX" || $coupon == "xxxxx") $price = 27.5; $fields['booking_price'] = $price; $where['booking_id'] = $booking['booking_id']; $wpdb->update($bookings_table, $fields, $where); } } return; }
//echo “<p>Event ID: $event_id</p>”;
$answers = eme_get_answers($booking_id);Thu 18 Sep 2014 at 19:35 #52851AnonymousInactiveHi, I tried a variation of this code. I do not need multiple amounts for a discount, just one discount of 5 dollars for each booking with a code. I have tried several different syntax groups, but entering the coupon code and clicking the submit button (takes a user to a paypal checkout), and the 5 dollar discount is not showing. Do you think you can help me out?
This is what I have now:
add_action('eme_insert_rsvp_action', 'my_eme_coupons',20,1); /** * Custom function to calculate coupon code discounts for events */ function my_eme_coupons($booking) { global $wpdb; $bookings_table = $wpdb->prefix.BOOKINGS_TBNAME; $discount = 5; $where = array(); $fields = array(); // Grab the coupon code from the extra answers $event_id = $booking['event_id']; $booking_id = $booking['booking_id']; $answers = eme_get_answers($booking_id); $coupon = $answers[0]['answer']; // Collect the event categories $event_cat = eme_get_event_categories($event_id); // As long as the coupon code isn't empty, look for matches if ($coupon != '') { // If coupon code used, apply the appropriate price change if ($coupon == "WCMDCPrice") { $price = ($booking['booking_price']) - ($discount); } $fields['booking_price'] = $price; $where['booking_id'] = $booking['booking_id']; $wpdb->update($bookings_table, $fields, $where); } return; }
Thu 18 Sep 2014 at 23:48 #52852FrankyKeymasterTry the following, but also make sure that
$answers[0]['answer']
is the answer containing your coupon code (you can always just do ‘print_r($answers);” to see what is in that array. Maybe also post your rsvp form template being used.add_action('eme_insert_rsvp_action', 'my_eme_coupons',20,1); /** * Custom function to calculate coupon code discounts for events */ function my_eme_coupons($booking) { global $wpdb; $bookings_table = $wpdb->prefix.BOOKINGS_TBNAME; $discount = 5; $where = array(); $fields = array(); // Grab the coupon code from the extra answers $event_id = $booking['event_id']; $booking_id = $booking['booking_id']; $answers = eme_get_answers($booking_id); $coupon = $answers[0]['answer']; // Collect the event categories $event_cat = eme_get_event_categories($event_id); // As long as the coupon code isn't empty, look for matches if ($coupon == "WCMDCPrice") { // If coupon code used, apply the appropriate price change $price = $booking['booking_price'] - $discount; $fields['booking_price'] = $price; $where['booking_id'] = $booking['booking_id']; $wpdb->update($bookings_table, $fields, $where); } return; }
Fri 19 Sep 2014 at 18:24 #52855AnonymousInactiveI tried with the answer being 0, then again with answer being 1, since the form field for the coupon has an id of 1, like this, and it did not work:
function my_eme_coupons($booking) { global $wpdb; $bookings_table = $wpdb->prefix.BOOKINGS_TBNAME; $discount = 5; $where = array(); $fields = array(); // Grab the coupon code from the extra answers $event_id = $booking['event_id']; $booking_id = $booking['booking_id']; $answers = eme_get_answers($booking_id); $coupon = $answers[1]['answer']; // Collect the event categories $event_cat = eme_get_event_categories($event_id); // As long as the coupon code isn't empty, look for matches if ($coupon == "WCMDCPrice") { // If coupon code used, apply the appropriate price change $price = $booking['booking_price'] - $discount; $fields['booking_price'] = $price; $where['booking_id'] = $booking['booking_id']; $wpdb->update($bookings_table, $fields, $where); } return;
This is what the form HTML looks like:
<div id=’eme-rsvp-message’>
<form id=’eme-rsvp-form’ name=’booking-form’ method=’post’ action=’#eme-rsvp-message’>
<div class=’eme-rsvp-form’>
Name*:<br><input required=’required’ type=’text’ name=’bookerName’ value=’Webmaster’ /><div class=’eme-required-field’> (Required field)</div><br>
E-Mail*:<br><input required=’required’ type=’text’ name=’bookerEmail’ value=’tech@roguevalleyhosting.com’ /><div class=’eme-required-field’> (Required field)</div><br>
Phone number:<br><input type=’text’ name=’bookerPhone’ value=” /><br>
Seats*:<br><select id=’bookedSeats’ name=’bookedSeats’></select><br>
Member Coupon:<br><input type=’text’ name=’FIELD1′ value=” /><br>
Comment:<br><textarea name=’bookerComment’></textarea><br>
<input type=’submit’ value=’Continue’ />
</div><input type=”hidden” id=”eme_rsvp_nonce” name=”eme_rsvp_nonce” value=”34baac4e73″ /><span id=’honeypot_check’>Keep this field blank: <input type=’text’ name=’honeypot_check’ value=” /></span>
<p>(* marks a required field)</p>
<input type=’hidden’ name=’eme_eventAction’ value=’add_booking’ />
<input type=’hidden’ name=’event_id’ value=’10’ />
</form>I also tried it this way: $coupon = $answers[FIELD1][‘answer’]; that did not work.
Any more help would be greatly appreciated. Thank you.
Sat 20 Sep 2014 at 18:47 #52866FrankyKeymasterPlease mark your code as such using the code button.
Also, as said: try to print_r($answers) to see the whole array.
Also, you can always try this (replace “MY_FIELD_NAME” with the name of the formfield you defined):function my_eme_coupons($booking) { global $wpdb; $bookings_table = $wpdb->prefix.BOOKINGS_TBNAME; $discount = 5; $where = array(); $fields = array(); // Grab the coupon code from the extra answers $event_id = $booking['event_id']; $booking_id = $booking['booking_id']; $answers = eme_get_answers($booking_id); $coupon = ""; foreach ($answers as $answer) { if ($answer['field_name'] == "MY_FIELD_NAME") { $coupon = $answer['answer']; } } // As long as the coupon code isn't empty, look for matches if ($coupon == "WCMDCPrice") { // If coupon code used, apply the appropriate price change $price = $booking['booking_price'] - $discount; $fields['booking_price'] = $price; $where['booking_id'] = $booking['booking_id']; $wpdb->update($bookings_table, $fields, $where); } return; }
Tue 23 Sep 2014 at 18:38 #52871AnonymousInactiveThank you so much for your help, my coupon is up and running.
Tue 20 Jan 2015 at 06:59 #53357AnonymousInactiveFranky,
I just added the code above to my functions.php with this modification
$coupon = ""; foreach ($answers as $answer) { if ($answer['field_name'] == "Coupon") { $coupon = $answer['answer']; } } [....] if ($coupon == "DISCOUNT10" || $coupon == "discount10") { // If coupon code used, apply the appropriate price change $price = $booking['booking_price'] - $discount;
and added a custom field
<tr><th scope='row'>#_FIELDNAME{9}:</th><td>#_FIELD{9}</td></tr>
to my booking form. Where the fieldname 9 equals “Coupon”.
Up to now I did nothing more, but there are no discounts made. Do I need anything else setup in order to get the calculation triggered?
Many thanks!
Cheers
JKTue 20 Jan 2015 at 10:04 #53360FrankyKeymasterDid you register the action function using add_action? E.g.:
add_action('eme_insert_rsvp_action', 'my_eme_discount_function',20,1);
Tue 20 Jan 2015 at 10:05 #53361AnonymousInactiveYes it is also in the functions.php.
Tue 20 Jan 2015 at 10:07 #53362AnonymousInactive/** * Add a hook for the Events Made Easy system to allow for coupon codes */ add_action('eme_insert_rsvp_action', 'my_eme_coupons',20,1); /** * Custom function to calculate coupon code discounts for events */ function my_eme_coupons($booking) { global $wpdb; $bookings_table = $wpdb->prefix.BOOKINGS_TBNAME; $discount = 15; $where = array(); $fields = array(); // Grab the coupon code from the extra answers $event_id = $booking['event_id']; $booking_id = $booking['booking_id']; $answers = eme_get_answers($booking_id); $coupon = ""; foreach ($answers as $answer) { if ($answer['field_name'] == "Coupon") { $coupon = $answer['answer']; } } // As long as the coupon code isn't empty, look for matches if ($coupon == "DISCOUNT10" || $coupon == "discount10") { // If coupon code used, apply the appropriate price change $price = $booking['booking_price'] - $discount; $fields['booking_price'] = $price; $where['booking_id'] = $booking['booking_id']; $wpdb->update($bookings_table, $fields, $where); } return; }
That is all I added!
Tue 20 Jan 2015 at 15:04 #53365FrankyKeymasterThen do a print_r($answer) in there to see what is in the $answer array (and a “die;” afterwards, so php won’t continue and you should be able to see what is being printed).
Tue 20 Jan 2015 at 15:37 #53366AnonymousInactiveHello Franky,
Many thanks! It’s solved! The issue was typo in the Formular field itself so it could not match… Thanks again!
BTW: Is it possible to calculate the price with a discount of 10%?
Cheers
JKTue 20 Jan 2015 at 15:41 #53367FrankyKeymasterSure it is, just change the line
$price = $booking['booking_price'] - $discount;
to be any formula you want.Tue 20 Jan 2015 at 15:48 #53368AnonymousInactive$price = $booking['booking_price'] - ( $booking['booking_price'] * ($discount / 100));
does the job 😀Fri 17 Apr 2015 at 22:27 #54098AnonymousInactiveHi,
is there a way to compare the coupon code regardless of lower and upper chars?
Will this work?
$coupon = strtolower($coupon );
if ($coupon == “discount10”)…As said before I’m not a programmer and I’m unsure if the code is PHP?
Cheers
JKFri 17 Apr 2015 at 22:34 #54099AnonymousInactiveI bet it could also be done vv with
$coupon = strtoupper($coupon);
if ($coupon == “DISCOUNT10″)…right? I did not tried it yet as my page is already live and currently I can’t test it!?
Fri 17 Apr 2015 at 23:38 #54101FrankyKeymasterThose are both valid ways of doing it 🙂
Fri 17 Apr 2015 at 23:43 #54102AnonymousInactiveGreat to know I’ll implement it now!
Thanks Frank! -
AuthorPosts
- The forum ‘Tips’ is closed to new topics and replies.