• Resolved chochochocosensei

    (@chochochocosensei)


    I am trying to implement a sort of volume discount on a multiprice event.

    I teach kids, and if there is more than one child attending overall, I want to reduce the price by 500. Note this is not for each additional child, just once.

    When all the kids are the same price, then I just adjust the overall price to reflect the discount,
    $price = (($seats * $price) - 500)/$seats;
    as shown below based on the example in the faq for discounts. The relevant part is the else part of the
    if ($found == 1&&eme_is_multi($booking['booking_price'])) {
    section below.

    But now I want to charge older kids a different price. With a multiprice, I dont know how to adjust the price. What I want is if the total number of seats is more than one, regardless of the ages, to somehow take 500 off the price. If there is only one child, that child should be charged teh appropriate price. I can’t use the formula above because there are different seats and prices involved. There may be just one child for each price. I don’t care about the individual prices so much as the total price.

    I suppose there might be a way to use discounts, but I don’t know how, and I dont want parents to have to enter “coupons”. I just want to automatically take the discount. It would be nice to be able to show them in my RSVP form that they had x kids at price 1 x kids at price 2 and there was a discount taken, but just as long as I can tell them the final price.

    NOTE: I don’t need at this point to send the revised amounts to a payment vendor, as people pay at the door, but the question does come up.

    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'];
    
    //not sure why this is here, but somehow I needed it.
    header("X-XSS-Protection: 0"); 
    
      //initialize find loop. This is a loop that can be ignored now, but might be used later.
    	$found=0;
      // This section used to search for only a specific category to apply discount to.
      // Presently the actual check for a particular category is disabled. all categories work.
      // First, collect the event categories for this event
      $event_c = eme_get_event_categories($event_id);
      $event_ct = $event_c[0];
    
      foreach($event_c as $x => $x_value) {
    //disabled check, all categories
    	//if ($x_value['category_id'] == 3) {
    	  $found=1;
    	  //echo "We found it!!! ".$found."<br />";
    	//};
        //echo "<br>";
      };
      
       //echo "did we find it?".$found."<br />";
    
       if ($found == 1&&eme_is_multi($booking['booking_price'])) { 
    	 // echo "this is a multiprice event".eme_is_multi($booking['booking_price']);
    // THIS IS WHERE I WANT TO PUT MY NEW CODE.	 
       } else {
    
    	// echo "this is a single price event".eme_is_multi($booking['booking_price']);
    	
          $seats=$booking['booking_seats'];
          $price=$booking['booking_price'];
          
    
          // more than 2 seats, then the price is reduced 500
         if ($seats> 1) { 
    	 $price = (($seats * $price) - 500)/$seats;
    	  }
    
          $fields['booking_price'] = $price;
          $where['booking_id'] = $booking['booking_id'];
          $wpdb->update($bookings_table, $fields, $where);
       }
       return;
    }
Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Franky

    (@liedekef)

    I haven’t taken too much a look at your code yet, but

    
    $seats = eme_get_booked_seats($event_id);
         if ($seats> 1) { 
    	 $price = $price - 500;
    	  }
    

    Might be what you want …

    Thread Starter chochochocosensei

    (@chochochocosensei)

    Thanks for the quick reply. My code is awful, as I don’t really know how to write php. Mostly I am cribbing from your examples, and guessing my way through.
    All I have been able to do is identify where the right code should go to separate single price from multiprice. But I don’t know how to access the actual final cost in a multiprice event, or alternatively to get the number of seats at price1 and at price2, and what price1 and price2 are in a multiprice event, and then adjust the individual prices to reflect a discount (which I could calculate as follow:

    The first line seems to correctly determine the number of seats. However, $price gives “1000||1500” which is all the prices for the event. What I want to change is the final amount to be charged… which (in proto-code) is equal to the

     cost1= number of seats at price 1 * price1;
      cost2 = number of seats at price2 * price 2 ;
      finalcost = cost1 + cost2;
      newfinalcost= finalcost - 500;

    Using the method I used for single price, if I could find out those four amounts, I could probably adjust the multi- $price to a new value as:

    `$price='(cost1-500)/ (number of seats at price 1)”.’||’.'(cost2-500)/ (number of seats at price 2)’

    It would be simpler if I could just change the final total price (cost1+cost2) directly, but I don’t know how to do that.

    Thread Starter chochochocosensei

    (@chochochocosensei)

    P.S. I just realized that your line for $seats seems to actually be giving me the total number of seats booked for the event (overall), but I can fix that by changing
    $seats = eme_get_booked_seats($event_id);
    to
    $seats=$booking['booking_seats'];
    I get the number of seats for the booking, which is correct. Nonetheless, I really need the number of seats at each price, as I mentioned above.

    Plugin Author Franky

    (@liedekef)

    You’re correct about $seats, my bad. Sorry (not enough coffee I guess …).
    Now I checked your code a bit more, and it is wrong from the start: eme_insert_rsvp_action is done *after* the booking is entered in the database, so you’d need to update the booking in the db then …
    An easier method is indeed to use discounts, you can read about it here:
    https://www.e-dynamics.be/wordpress/discounts/
    You need to use the type “code” as discount type. There’s an example in the code there too. And there you can use:

    $seats=$booking['booking_seats'];
    if ($seats> 1) {
       return 500;
    } else {
       return 0;
    }
    Thread Starter chochochocosensei

    (@chochochocosensei)

    Again, I appreciate your time in giving me the answer.

    (the reason I was avoiding using discounts was that I didn’t want my customers to have to enter in a code of some sort. But from your description perhaps the code is just there as a placeholder in code type discounts, so they dont have to enter it in specifically??? But how? It is not working for me, yet…. )

    Let me see if I am understanding this.

    1. on my Event form my “discount to apply” is multipleChildDiscount

    2. I have a snippet entitled eme_discount_multipleChildDiscount activated. I used the code you gave me:

    function my_eme_discount_function($booking) {
      print_r($booking);
      $seats=$booking['booking_seats'];
      if ($seats > 1) {
    	echo "discount applied";
       return 500;
      } else {
    	echo "NO discount applied";
       return 0;
      }
    }
    add_filter('eme_discount_multipleChildDiscount','my_eme_discount_function');

    3. per instructions on the page you refered to, I added $_DISCOUNT to my RSVP form as follows (near end) (I think this is probably where i am going wrong):

    <br />
    <h1>Family Profile</h1>
    <table class="alignleft">
    <tbody>
    <tr><th>[member_only]Email: [/member_only]</th><td>[user_email]</td>
    </tr>
    <tr><th>[member_only]Parent's name:[/member_only]</th><td>[user_firstname]</td>
    </tr>
    <tr><th>[member_only]Children's Names/Ages:[/member_only]</th><td>[user_lastname]</td>
    </tr>
    <tr>
    <td style="text-align: center;" colspan="2" scope="row">To change this information, <a href="https://chocochoco.byethost4.com/wp/pm_profile/">change your profile</a>.</td>
    </tr>
    </tbody>
    </table>
    
    <p style="text-align: left;">If are already reserved for this event, it will appear below:</p>
    <div>[eme_mybookings id=#_EVENTID template_id=7 template_id_footer=8]</div>
    
    <h1>Register for the event</h1>
    <span style="display:none;" > #_NAME, #_EMAIL</span>
    <table class="eme-rsvp-form alignleft" style="width:100%;">
    <tbody>
    <tr>
    <th style="text-align: right;" scope="row">How many children under 3? *</th>
    <td>#_SEATS{1}</td>
    </tr>
    <tr>
    <th style="text-align: right;" scope="row">How many children 3 or over? *</th>
    <td>#_SEATS{2}</td>
    </tr>
    <tr>
    <th style="text-align: right;" scope="row">Comments, Questions, Suggestions</th>
    <td style="vertical-align:middle">#_COMMENT</td>
    </tr>
    #_CAPTCHAHTML{<tr><th scope='row'>Please fill in the code displayed here:</th><td>#_CAPTCHA</td></tr>}
    <tr>
    <th style="text-align: center;" colspan="2" scope="row">#_SUBMIT</th>
    </tr>
    <tr>
    <th style="text-align: center;" colspan="2" scope="row">#_DISCOUNT</th>
    </tr>
    </tbody>
    </table>

    4. I added #_TOTALDISCOUNT to my booking recorded form (in middle) as follows:

    <h2>Reservation Information</h2>
    <p style="">If there are any errors, please let me know.</p>
    <p style="">on #_BOOKINGCREATIONDATE?at #_BOOKINGCREATIONTIME</p>
    <p style="">#_RESPLASTNAME?registered the following information:</p>
    <p style="">children's names: #_RESPFIRSTNAME</p>
    <p style="">email: #_RESPEMAIL</p>
    <p style="">phone: #_RESPPHONE</p>
    <p style=""></p>
    <p style="">Booking ID: #_BOOKINGFIELD{booking_id}</p>
    <p style="">#_RESPSPACES{1} child(ren) for a total of?#_TOTALPRICE{1}</p>
    <p style="">#_RESPSPACES{2} child(ren) for a total of?#_TOTALPRICE{2}</p>
    <p style=""> for a total of #_RESPSPACES child(ren) and a grand total of?#_TOTALPRICE</p>
    <p style="">(discount applied if any?#_TOTALDISCOUNT)</p>
    
    <p style="">Comments:</p>
    <p>#_RESPCOMMENT</p>
    <p>--</p>
    <h2>Get Directions to the event here:</h2>
    <p style="">#_DIRECTIONS</p>
    [ls_content_block id="705"] | <a class="cancellink" href="#_CANCEL_OWN_URL" target="_blank">Cancel Reservation</a></p>
    <br /></p>
    <p style=""></p>
    <h2>All Your Bookings</h2>
    <p style="">[eme_mybookings template_id=7]</p>

    That’s all I can think of. When I actually run all this, total discount is always zero.

    Plugin Author Franky

    (@liedekef)

    Hmmm … try to remove #_DISCOUNT in your form, see what that gives … since you don’t need to enter any data, it is not really needed. Maybe I should handle that exception when you use the function-type discount …

    Thread Starter chochochocosensei

    (@chochochocosensei)

    Thanks again. I apologize if my poor programming skills are making this difficult.

    Removing the #_DISCOUNT had no effect.

    I should have added that I added the discount as follows:

    name:multipleChildDiscount, description:multiple kids, Discountgroup:(blank), coupon:xxx, casesentitive:No, value:500, Type:Code, maxusage:1, usage: 1, expiration (blank)

    I tried it with a coupon value and with a blank, since I really don’t understand how to handle the coupon code, as I don’t need it.

    also, the print_(bookings) in my code never printed anywhere. When I was using the add_action('eme_insert_rsvp_action', 'my_eme_discount_function',20,1); (see below)
    my echoes and prints would appear briefly at the top of the page and then be overwritten by the actual booking recorded message. I wish I could make them more permanent. I understand this is because these are happening at different times, but I would like to be able to carry over some message from the discount to the booking recorded message.

    Meanwhile, even though you discouraged me, I did manage to get the insert rsvp method to work (despite the poor programming and excessive debugging code). See below, with one problem, how to show that the discount has been applied. Using?#_TOTALDISCOUNT doesn’T seem to work.

    I would still prefer to use your suggested method, but I cont seem to make it work.

    here is my my_eme_discount_function

    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'];
    header("X-XSS-Protection: 0");
      //initialize
    	$found=0;
      // This section searches for only a specific category
      // Presently the actual check for a particular category is disabled. all categories work.
      // First, collect the event categories for this event
      $event_c = eme_get_event_categories($event_id);
      $event_ct = $event_c[0];
      //echo  "count".count($event_ct);
     // echo "event_ct[0]".$event_ct[0]."<br />";
      //echo "event_ctcategory_id]".$event_ct['category_id'].'<br />';
      foreach($event_c as $x => $x_value) {
        //echo "Key=" . $x . ", Value=" . $x_value. ", Value[category_id]=" . $x_value['category_id'];
    	//if ($x_value['category_id'] == 3) {
    	  $found=1;
    	  //echo "We found it!!! ".$found."<br />";
    	//};
        //echo "<br>";
      };
      
       //echo "did we find it?".$found."<br />";
    
     
    	 /* you can put in the event_id that needs processing, or leave out this line to process all events  or do as follows */
       if ($found == 1&&eme_is_multi($booking['booking_price'])) { 
    	 echo "this is a multiprice event".eme_is_multi($booking['booking_price']);
    	 echo '<br />';
         echo '<br />total seats <br />';
    	 $totalseats=$booking['booking_seats'];
    	 print_r($totalseats);
    	 
    	 $seats=eme_convert_multi2array($booking['booking_seats_mp']);
    	 
    	 $price=$booking['booking_price'];
    	  echo "<br/>$booking[booking_price]<br />";
    	 print_r($price);
    	 $arrayprice=eme_convert_multi2array($price);
    	 
    	 echo '<br />seats <br />';
    	 print_r($seats);
    	 echo '<br />arrayprice <br />';
    	 print_r($arrayprice);
    	 
    	 if ($totalseats> 1) { 
    	   if ($seats[0] > 0) {
    	    $arrayprice[0] = (($seats[0]* $arrayprice[0]) - 500)/$seats[0];
    		 echo "<br />changed first seat";
    	   } else {
    		$arrayprice[1] = (($seats[1]* $arrayprice[1]) - 500)/$seats[1];
    		 echo "<br />changed second seat";
    	   };
    	  };
    	 echo '<br />seats after <br />';
    	 print_r($seats);
    	 echo '<br />arrayprice after <br />';
    	 print_r($arrayprice);
    	 echo '<br />BOOKING AFTER <br />';
    	 print_r($booking);
    	 
    	 $price = $arrayprice[0]."||".$arrayprice[1];
    	 echo '<br/>$price<br />';
    	 print_r($price);
         
    	 	 
    	 $fields['booking_price']=$price;
    	 $where['booking_id']=$booking['booking_id'];
         $wpdb->update($bookings_table, $fields, $where);
    	 
       } else {
    	 
    	// echo "this is a single price event".eme_is_multi($booking['booking_price']);
    	
          //echo 'EVENT ID='.$event_id .' Booking_id = '.$booking['booking_id'];
          //echo 'category id = '.$event_ct['category_id'].'category name = '.$event_ct['category_name'];
          $seats=$booking['booking_seats'];
          $price=$booking['booking_price'];
    	 print_r($seats);
    	 print_r($price);
           //   echo " seats=".$seats;
             echo " price=".$price;  
          // more than 2 seats, then the price is 500 off
         if ($seats> 1) {
          $price = (($seats * $price) - 500)/$seats;
    	  };
             // echo $seats;
             echo " after price=".$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);
    	 
    	  echo '<br />BOOKING AFTER <br />';
          print_r($booking);
       }
       return;
    }
    Plugin Author Franky

    (@liedekef)

    Well, when using filters, echo/print_r etc … aren’t shown unless you stop the execution there (by e.g. using “die”).
    Now, I just tried this in my functions.php:

    
    function my_eme_discount_function($booking) {
      $seats=$booking['booking_seats'];
      if ($seats > 1) {
       return 500;
      } else {
       return 0;
      }
    }
    add_filter('eme_discount_multipleChildDiscount','my_eme_discount_function');
    

    (in fact: your code, but yours seemed to contain a weird character after “$seats”).
    And I configured the discount as you did, but set the maxusage to 0 (otherwise it can only be used once …).
    The result seemed to be what is expected (500 given as discount).
    Now I’ve seen that the calculated discount is not shown in the rsvp overview screens as a column, I’ve corrected that for the next version.

    Thread Starter chochochocosensei

    (@chochochocosensei)

    Thanks for edifying me…I will try again with your revision and suggestions in a day or two as my site is temporarily down for other reasons…but I really appreciate your going to the trouble and hope to be able to report I have it working….

    The weird character is probably a japanese space. I often switch between japanese and US keyboard, and probably a stray space got introduced.

    Plugin Author Franky

    (@liedekef)

    Well, by that time a new release might have happened already ??
    Hope you get it to work!

    Thread Starter chochochocosensei

    (@chochochocosensei)

    well waddya know, after crashing my site offline twice, several bouts of ftp skulduggery, and assorted yadda yadda, it works as advertised. All caused by those tiny little the stray characters I am very sorry about. I don’t think I ever would have every figured it out without your mentioning them. There were actually several and get this it was impossible to delete or backspace them away! They would reappear before my eyes!! on both my computer and tablet! I think they were either stray japanese characters, or some stray introduced via cut and paste, or demons from another planet. I ended up just retyping it all from scratch, and between that and fixing the maxusage, lo and behold we have discountabilibility. As for your help, it was full value, and thank you very much for support above and beyond the call of duty. I am truly grateful…and a donation has been sent your way! Keep up the good work!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘volume discount’ is closed to new replies.