Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Adjusts the EM Coupon Discount based on number of booked tickets.
- *
- * If you have an EM Coupon with "2 for 1" and someone books 5 tickets, they should only get the discount for 4 tickets.
- * Events Manager Pro can only calculate the discount over the complete booking. This snippets will alter that.
- * Requires: An existing EM Coupon with 50% discount.
- *
- * @param object $EM_Event The current Events Manager Event object.
- * @param object $EM_Booking The current Events Manager Booking object.
- * @return object
- */
- function stonehenge_change_coupon_value( $EM_Event, $EM_Booking ) {
- // Change '2for1' to your the actual coupon code in your database.
- if( isset( $EM_Booking->booking_meta['coupon'] ) && '2for1' === $EM_Booking->booking_meta['coupon']['coupon_code'] ) {
- // Only change the 50% discount if the number of tickets is an odd number.
- $odd = $EM_Booking->spaces % 2 == 0 ? false : true;
- if( $odd ) {
- // Get raw booking price.
- $EM_Tickets = $EM_Booking->get_tickets_bookings();
- foreach( $EM_Tickets as $EM_Ticket ) {
- $total = $EM_Tickets->get_price( false ); // Raw value: total without discount and tax.
- }
- $single_ticket_price = ($raw_total / $spaces);
- // Calculate the allowed amount of discount.
- $discount_spaces = ($spaces - 1); // Make even -> exclude 1 ticket that does not get the discount.
- $discountable = ($single_ticket_price * $discount_spaces);
- $discount_value = ($discountable / 2); // 50% discount.
- // Send new discount to booking.
- $EM_Booking->booking_meta['coupon']['coupon_type'] = '#'; // Fixed amount.
- $EM_Booking->booking_meta['coupon']['coupon_discount'] = $discount_value;
- $EM_Booking->booking_meta['coupon']['coupon_tax'] = 'pre'; // We calculated excluding taxes.
- }
- }
- return $EM_Booking;
- }
- add_action( 'em_booking_add', 'stonehenge_change_coupon_value', 11, 2 );
Advertisement
Add Comment
Please, Sign In to add comment