StonehengeCreations

Events Manager Pro – Calculate Coupon "2 for 1"

Feb 24th, 2020 (edited)
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.89 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Adjusts the EM Coupon Discount based on number of booked tickets.
  4.  *
  5.  * If you have an EM Coupon with "2 for 1" and someone books 5 tickets, they should only get the discount for 4 tickets.
  6.  * Events Manager Pro can only calculate the discount over the complete booking. This snippets will alter that.
  7.  * Requires: An existing EM Coupon with 50% discount.
  8.  *
  9.  * @param  object $EM_Event   The current Events Manager Event object.
  10.  * @param  object $EM_Booking The current Events Manager Booking object.
  11.  * @return object
  12.  */
  13. function stonehenge_change_coupon_value( $EM_Event, $EM_Booking ) {
  14.     // Change '2for1' to your the actual coupon code in your database. 
  15.     if( isset( $EM_Booking->booking_meta['coupon'] ) && '2for1' === $EM_Booking->booking_meta['coupon']['coupon_code'] ) {
  16.        
  17.         // Only change the 50% discount if the number of tickets is an odd number.
  18.         $odd = $EM_Booking->spaces % 2 == 0 ? false : true;
  19.  
  20.         if( $odd ) {
  21.             // Get raw booking price.
  22.             $EM_Tickets = $EM_Booking->get_tickets_bookings();
  23.             foreach( $EM_Tickets as $EM_Ticket ) {
  24.                 $total = $EM_Tickets->get_price( false );   // Raw value: total without discount and tax.
  25.             }
  26.             $single_ticket_price = ($raw_total / $spaces);
  27.  
  28.             // Calculate the allowed amount of discount.
  29.             $discount_spaces    = ($spaces - 1);    // Make even -> exclude 1 ticket that does not get the discount.
  30.             $discountable       = ($single_ticket_price * $discount_spaces);
  31.             $discount_value     = ($discountable / 2); // 50% discount.
  32.            
  33.             // Send new discount to booking.
  34.             $EM_Booking->booking_meta['coupon']['coupon_type'] = '#';   // Fixed amount.
  35.             $EM_Booking->booking_meta['coupon']['coupon_discount'] = $discount_value;
  36.             $EM_Booking->booking_meta['coupon']['coupon_tax'] = 'pre'; // We calculated excluding taxes.
  37.         }
  38.     }
  39.     return $EM_Booking;
  40. }
  41. add_action( 'em_booking_add', 'stonehenge_change_coupon_value', 11, 2 );
Advertisement
Add Comment
Please, Sign In to add comment