Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Author Franky

    (@liedekef)

    See the doc site for discount code examples:
    https://www.e-dynamics.be/wordpress/?p=305

    Thread Starter fcvolunteer

    (@fcvolunteer)

    Hi Franky,

    Just wanted to double check that the code below is the applicable code and I also wanted to clarify how I set what the acceptable coupon code is. Thanks!

    add_action('eme_insert_rsvp_action', 'my_eme_discount_function',20,1);
    function my_eme_discount_function($booking) {
       global $wpdb;
       $bookings_table = $wpdb->prefix.BOOKINGS_TBNAME;
       $where = array();
       $fields = array();
    
       $booking_id = $booking['booking_id'];
       $event_id = $booking['event_id'];
    
       if ($event_id == 5) {        /* put in the event_id that needs processing, or leave out this line to process all events */
          //echo 'EVENT ID='.$event_id .' Booking_id = '.$booking['booking_id'];
    
          $seats=$booking['booking_seats'];
          $price=$booking['booking_price'];
    
            // below a small example to check own input
    		$coupon = "";
            $answers = eme_get_answers($booking_id);
            foreach ($answers as $answer) {
                if ($answer['field_name'] == "MY_FIELD_NAME") {
                   $coupon = $answer['answer'];
                }
            }
            // now check $coupon for your wanted value
    
          $fields['booking_price'] = $price;
          $where['booking_id'] = $booking['booking_id'];
          $wpdb->update($bookings_table, $fields, $where);
       }
       return;
    }
    
     ?>
    Plugin Author Franky

    (@liedekef)

    In the forum on my site you find some examples. This one vies a discount of 10% if a custom field called “Coupon” contains the text “COUPON10”:

    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 = 10;
    	$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'];
                }
            }
    	if ($coupon == "COUPON10") {
    		// If coupon code used, apply the appropriate price change
    		$price = $booking['booking_price'] - ( $booking['booking_price'] * ($discount / 100));
    
    		$fields['booking_price'] = $price;
    		$where['booking_id'] = $booking['booking_id'];
    		$wpdb->update($bookings_table, $fields, $where);
    	}
    	return;
    }
    Thread Starter fcvolunteer

    (@fcvolunteer)

    Thanks so much Franky! One last question. How can I get it to deduct the price of one ticket from the entire amount? for example if it was $20/ticket and someone entered the code and was booking only 1 space it would be free but if someone else was booking 3 spots and entered the code it would be $40 instead of $60.

    Thanks!

    Plugin Author Franky

    (@liedekef)

    The price for a single seat is in $event[‘price’] , so:

    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 = 10;
    	$where = array();
    	$fields = array();
    
    	// Grab the coupon code from the extra answers
    	$event_id = $booking['event_id'];
    	$event = eme_get_event($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'];
                }
            }
    	if ($coupon == "COUPON10") {
    		// If coupon code used, apply the appropriate price change
    		$price = $booking['booking_price'] - $event['price'];
    
    		$fields['booking_price'] = $price;
    		$where['booking_id'] = $booking['booking_id'];
    		$wpdb->update($bookings_table, $fields, $where);
    	}
    	return;
    }
    Thread Starter fcvolunteer

    (@fcvolunteer)

    After using the code below

    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 = 25;
       $where = array();
       $fields = array();
    
    // Grab the coupon code from the extra answers
       $event_id = $booking['event_id'];
       $event = eme_get_event($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'];
                }
            }
    
    		if ($coupon == "TeenFriend") {
    	// If coupon code used, apply the appropriate price change
    	$price = $booking['booking_price'] - $event['price'];
    
            // now check $coupon for your wanted value
    
          $fields['booking_price'] = $price;
          $where['booking_id'] = $booking['booking_id'];
          $wpdb->update($bookings_table, $fields, $where);
       }
       return;
    }

    it stopped forwarding visitors to the paypal payment page and is discounting the entire booking amount instead of just discounting the event price.

    What am I doing wrong?

    TIA!

    Plugin Author Franky

    (@liedekef)

    The discount code is for a single-price event, maybe you’re using something else?
    Also: if the price is 0, then the payment page is not shown of course.

    Thread Starter fcvolunteer

    (@fcvolunteer)

    The event is set to be single-price the only amount I have listed for price is 25. I understand that if someone is only purchasing a single ticket and uses the discount code the price would equal 0 and they wouldn’t be brought to the payment page but that doesn’t explain why someone who books 4 tickets isn’t being charged for 3 and being brought to the payment page.

    Any other ideas?

    Plugin Author Franky

    (@liedekef)

    I don’t see anything wrong with this code, so maybe send me a email with login details so I can see for myself.

    Thread Starter fcvolunteer

    (@fcvolunteer)

    How do I find your email?

    Thanks so much!

    Plugin Author Franky

    (@liedekef)

    My email : [email protected]

    Thread Starter fcvolunteer

    (@fcvolunteer)

    Emailed you. Thanks!

    Plugin Author Franky

    (@liedekef)

    This is the code that works, since in fact the booking_price is the price per seat in a booking, not the total price.
    Maybe it is useful to add a total price booking, or a discount field …

    add_action('eme_insert_rsvp_action', 'FCNC_eme_coupons',20,1);
    /**
     * Custom function to calculate coupon code discounts for events
     */
     function FCNC_eme_coupons($booking) {
       global $wpdb;
       $bookings_table = $wpdb->prefix.BOOKINGS_TBNAME;
       $discount = 25;
       $where = array();
       $fields = array();
    
    // Grab the coupon code from the extra answers
       $event_id = $booking['event_id'];
       $event = eme_get_event($event_id);
       $booking_id = $booking['booking_id'];
       $answers = eme_get_answers($booking_id);
       $seats = $booking['booking_seats'];
    
       $coupon = "";
            foreach ($answers as $answer) {
                if ($answer['field_name'] == "Coupon") {
                   $coupon = $answer['answer'];
                }
            }
    
       if ($coupon == "TeenFriend" && $seats>1) {
          // If coupon code used, apply the appropriate price change
          $price = $booking['booking_price']*($seats-1)/$seats;
          $price = sprintf("%01.2f",$price);
    
          // now check $coupon for your wanted value
          $fields['booking_price'] = $price;
          $where['booking_id'] = $booking_id;
          $wpdb->update($bookings_table, $fields, $where);
       }
       return;
    }

    Thread Starter fcvolunteer

    (@fcvolunteer)

    Thanks!!!

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Discount Code’ is closed to new replies.