Events Made Easy Forums Tips Coupon Codes

  • This topic has 19 replies, 4 voices, and was last updated 9 years ago by Anonymous.
Viewing 20 posts - 1 through 20 (of 20 total)
  • Author
    Posts
  • #52201
    Anonymous
    Inactive

    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;
    }
    
    #52206
    Franky
    Keymaster

    Thanks for sharing !

    #52249
    Anonymous
    Inactive

    Just 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);

    #52851
    Anonymous
    Inactive

    Hi, 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;
    }
    
    #52852
    Franky
    Keymaster

    Try 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;
    }
    
    #52855
    Anonymous
    Inactive

    I 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.

    #52866
    Franky
    Keymaster

    Please 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;
    }
    
    #52871
    Anonymous
    Inactive

    Thank you so much for your help, my coupon is up and running.

    #53357
    Anonymous
    Inactive

    Franky,

    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
    JK

    #53360
    Franky
    Keymaster

    Did you register the action function using add_action? E.g.:
    add_action('eme_insert_rsvp_action', 'my_eme_discount_function',20,1);

    #53361
    Anonymous
    Inactive

    Yes it is also in the functions.php.

    #53362
    Anonymous
    Inactive
    /**
     * 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!

    #53365
    Franky
    Keymaster

    Then 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).

    #53366
    Anonymous
    Inactive

    Hello 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
    JK

    #53367
    Franky
    Keymaster

    Sure it is, just change the line
    $price = $booking['booking_price'] - $discount;
    to be any formula you want.

    #53368
    Anonymous
    Inactive

    $price = $booking['booking_price'] - ( $booking['booking_price'] * ($discount / 100)); does the job 😀

    #54098
    Anonymous
    Inactive

    Hi,

    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
    JK

    #54099
    Anonymous
    Inactive

    I 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!?

    #54101
    Franky
    Keymaster

    Those are both valid ways of doing it 🙂

    #54102
    Anonymous
    Inactive

    Great to know I’ll implement it now!
    Thanks Frank!

Viewing 20 posts - 1 through 20 (of 20 total)
  • The forum ‘Tips’ is closed to new topics and replies.
Scroll to Top