Events Made Easy Forums How do I … activate Coupon code on AJAX callback

Viewing 18 posts - 1 through 18 (of 18 total)
  • Author
    Posts
  • #58415
    Anonymous
    Inactive

    Franky,
    So I have client that needs me to make a call to a remote table to see if the email entered on the RSVP form matches someone in their system. I can do that no problem with an AJAX call grabbing the email and sending it off. When I get the JSON call back that it matches I need to apply a discount code. My effort now is to use a simple jQuery call and populate the coupon field with the correct coupon word (“Gold” in this case). When I auto populate the coupon field on the RSVP form it doesn’t trigger any action to change the total cost. I have even tried to trigger keydown, keyup events but nothing seems to work. How can I dynamically fill in that coupon field and get the cost to change? What call do you have on that .dynamicprice field to trigger it to look and see if it matches? Am I going about this wrong? Thanks.

    #58416
    Franky
    Keymaster

    Use the available hooks for that. See https://www.e-dynamics.be/wordpress/category/documentation/17-discounts/ (discount of type “code”).
    Tip: to get the person related to the booking:
    $person = eme_get_person($booking[‘person_id’]);
    And then you can check $person[’email’]

    #58417
    Anonymous
    Inactive

    Thanks Franky. So can you tell me when this hook should trigger? I have it set to code type and a 50% discount. When I fill out the form and submit my RSVP the price stays the same. Should it happen on the submission of my booking?
    Jon

    function my_eme_discount_function($booking) {
    $answers=eme_get_booking_answers($booking[‘booking_id’]);
    foreach ($answers as $answer) {
    $coupon = $answer[‘answer’];
    }
    return $calculated_discount;
    }
    add_filter(’eme_discount_gold’,’my_eme_discount_function’);

    #58418
    Franky
    Keymaster

    Based on your example: Did you create a discount called “gold” with type “code”? And did you assign that discount to your event?

    #58422
    Anonymous
    Inactive

    Thanks Franky. I did create the code and apply. I think I have actually come up with another solution but I don’t know that I have seen documentation on one thing… How can I get the list of coupon codes available on the event as it loads? For instance, If I group 3 discounts into a discount group and put them on an event, where can I access the coupon codes when the page loads and store them? I am leaving the site and gathering information about users. When I come back I need to compare what I have to coupons that are available on that page. Can I get them into an array on load?

    #58423
    Franky
    Keymaster

    Messing around with the discount tables in EME is not recommended. It is the responsibility of EME to handle those. If you use a discount of type code and follow the examples in the doc, then you can leave the site in your ajax call, check whatever you want and come back with the result.
    For “regular” discounts (not type code), I don’t support changing the tables or evaluating things on your own. If you just want to check the user info and do some evaluation, you can also just use the rsvp filter eme_eval_booking_form_post_filter. See the doc: https://www.e-dynamics.be/wordpress/category/documentation/12-hooks-and-filters/

    #58424
    Anonymous
    Inactive

    Oh I dont want to mess with the tables at all I just want to know if there is a hook where I can see the coupon codes and store them in an array when the page loads.
    Example: I have 10 different member types in this third party database. However this event only offers discounts to 3 types of members. When I go and see if the email is a match in the third party and come back I need to see if that member type has a coupon available. If not I want to write something like “I see you are a member unfortunately,…… So to have those coupons available for this event stored would be great!

    #58427
    Franky
    Keymaster

    Based on your requirement, I think you could take a look at eme_insert_rsvp_action to set $booking[‘discount’] to 0 after your evaluation …

    #58436
    Anonymous
    Inactive

    I don’t think I am explaining this very well. When the RSVP page loads are the coupon codes available or do you not even go and look for them until they start typing in the coupon field. All I need to know is if I can get the name of the coupon(s) for each event when the RSVP page loads. If I have a discount on a page with Name:’Gold Status’ and Coupon:’gold’ and Value:’10’ is there anyway I can see these values? They have to be available but eme_insert_rsvp_action executes after inserting into the database.
    I’d be happy to chat about this offline if this gets to be to tedious in scope.
    Here is my example in general terms of what I need

    add_action(‘WHAT_EME_ACTION_LOADS_RSVP_FORM’, ‘LOOK_FOR_CODES’,20,1);
    function LOOK_FOR_CODES (){
    //look for each discount applied to the page
    for each discount applied to this page {
    couponcode[]=each discount code
    }
    }
    I can then access couponcode array later to do some work not in the EME framework. I just need to know those codes when it loads. Thanks Franky!

    #58437
    Franky
    Keymaster

    When the page loads, I don’t take a look at the defined codes. They only get loaded and evaluated upon post (or when using #_DYNAMIC_PRICE).
    You can however use the $event variable (that has the discount_id and discount group id in it’s array), but the evaluation of the discount happens when someone submits the form (you can’t interfere there, unless by using the hooks I mentioned above).

    #58438
    Anonymous
    Inactive

    Cool. I need the discount to happen on the submission, that is fine. If I can get the discount_id or discount group ID I can make a call to the tables and figure out the values I need. How do I access the $event variable?
    This is going to be WAY cool if this works!
    Jon

    #58439
    Franky
    Keymaster

    Use the hook eme_insert_rsvp_action , the $booking variable has the event id (and in fact also the applied discount ids, the discount codes entered and the discount codes used, see at the top of eme_rsvp.php).
    But at that time, the discount is already applied (like I said before), so you just need to use that action to set the discount back to 0 (and erase discount ids or so) if your user/email check says so (imho you don’t even need to check the discount ids). See the doc on eme_insert_rsvp_action (it has a discount example even).

    #58441
    Anonymous
    Inactive

    so I have gone a round about way to get the coupons code on page load. I have hijacked the event ID from the permalink and made a call to the database manually. I can get the events_properties. What format is that in? if it is an array, what is it delimited by? I am home free now that I have gotten in here but I am trying to figure out in what format you have that in! Thanks!
    jon

    #58443
    Franky
    Keymaster

    use eme_get_event($id) to load the event in a readable format (array then, use print_r or var_dump to inspect).
    And to get the event id at load time, just use something like this:

       if (eme_is_single_event_page()) {
          // single event page
          $event_id = intval(get_query_var('event_id'));
       }
    #58446
    Anonymous
    Inactive

    oh you simply have it serialized. I unserialized and made my way. Thanks for the guidance. Just for future reference. (all done outside of EME functions)
    1. Grab the event ID from the URL.
    2. Query the eme_events table with the id and grab the properties
    3. Unserialize the properties and pull out the discount group and discount
    4. Check to see which is applied.
    5. Grab the names of the coupons.
    6. Return these names in a json ajax call.
    7. From the naming convention of these codes I can tell which group of members actually has a coupon for the event. This way I can give them the “We see that you are a member. Unfortunately, no code exists for this event.” If the code does exist I return the code and they apply. dynamic costs takes over at that point.
    Thank you so much!
    Jon

    #58449
    Franky
    Keymaster

    Well, using EME functions (and get_query_var), point 1, 2 and 3 are easier 🙂 And probably also 4 and 5 🙂
    You never mentioned you needed the discount names to base your member check on (I thought you wanted to use the person name/email and check membership based on that). Anyway, using eme_get_event is a supported API call (as are others: eme_get_person, eme_get_discount, etc …), so in case any DB change might occur: I recommend you try to use the basic eme functions as much as possible.

    #58450
    Anonymous
    Inactive

    I couldn’t figure out how to call them outside the scope of EME. I need to call them on an AJAX call. How do you call them? I’d love to stay in scope!

    #58457
    Franky
    Keymaster

    As long as you stay inside the wp ajax functions (so e.g. the eme filter rsvp filter), then you can just use those functions. If not, then you need to initialize wp:

    
    define( 'WP_USE_THEMES', false ); // Don't load theme support functionality
    require( './wp-load.php' );
Viewing 18 posts - 1 through 18 (of 18 total)
  • The forum ‘How do I …’ is closed to new topics and replies.
Scroll to Top