Advertisement
eventsmanager

Surcharges and Discounts

Aug 21st, 2017 (edited)
2,687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.09 KB | None | 0 0
  1. <?php
  2. /*
  3. This Snippet shows how to add surcharges and discounts to all bookings. Adjust accordingly.
  4.  
  5. Requires EM Pro 3.2.10 to work in MB mode.
  6.  
  7. For more information on how to add this snippet, please see : http://wp-events-plugin.com/tutorials/how-to-safely-add-php-code-to-wordpress/
  8. */
  9. class EM_Price_Adjustments {
  10.     public static function init() {
  11.         // Add adjustments to events or a whole MB booking, currently EM will not support adding individual charges to individual bookings in MB mode
  12.         if( get_option('dbem_multiple_bookings') ) {
  13.             // Add adjustments to a whole Multiple Booking, not individual events
  14.             add_action('em_multiple_booking_updated', array('EM_Price_Adjustments', 'add_mb_price_adjustments'), 10, 3);
  15.         } else {
  16.             // Add adjustments to the inidial default price if there are any pre-selected tickets.
  17.             add_filter('em_bookings_get_intent_default', array('EM_Price_Adjustments', 'get_intent_default'), 10);
  18.             // Adds price adjustments to individual event bookings, when booking is submitted (or submitted for a summary)
  19.             add_filter('em_booking_get_post', array('EM_Price_Adjustments', 'add_price_adjustments'), 1, 2);
  20.         }
  21.        
  22.     }
  23.     /**
  24.      * Example function to show how surcharges and discounts can be added to a booking.
  25.      * @param boolean $result If previous post processing woas successful
  26.      * @param EM_Booking $EM_Booking The Booking object being modified.
  27.      * @return bool
  28.      */
  29.     public static function add_price_adjustments( $result, $EM_Booking ){
  30.         //Only apply surcharge if booking has passed validation and therefore can be saved to database
  31.         //You could also do further checks here if you want to give discounts to specific events or bookings
  32.         if( $result ){
  33.             //Ensure we have arrays assigned to booking meta, if not create them to avoid PHP warnings
  34.             if( empty($EM_Booking->booking_meta['surcharges']) ) $EM_Booking->booking_meta['surcharges'] = array();
  35.             if( empty($EM_Booking->booking_meta['discounts']) ) $EM_Booking->booking_meta['discounts'] = array();
  36.            
  37.             // Add some adjustments, note that we're adding unique keys to the pricing adjustments, so that if we need to add/recalculate we don't add more than once.
  38.            
  39.             //Example Surcharges
  40.            
  41.             //This one adds a fixed $25 surcharge and is applied before taxes
  42.             $EM_Booking->booking_meta['surcharges']['special'] = array(
  43.                 'name' => 'Special Surcharge',
  44.                 'desc' => 'Some type of surcharge description',
  45.                 'type' => '#', //numerical discount i.e. $10.00 off
  46.                 'amount' => '25.00',
  47.                 'tax' => 'pre' //discount applied BEFORE taxes have been added, and IS taxable
  48.             );
  49.             //This one adds a %3 surcharge, and is applied after taxes.
  50.             $EM_Booking->booking_meta['surcharges']['handling_fee'] = array(
  51.                 'name' => 'Handling Fee',
  52.                 'desc' => 'Some type of surcharge description',
  53.                 'type' => '%', //percentage discount of total price after taxes i.e. %3 extra
  54.                 'amount' => '3.00',
  55.                 'tax' => 'post' //discount applied AFTER taxes have been added, and IS NOT taxable
  56.             );
  57.            
  58.             //Example Discounts
  59.            
  60.             //This one adds a %3 discount before taxes have been applied.
  61.             $EM_Booking->booking_meta['discounts']['handling_fee'] = array(
  62.                 'name' => 'Handling Fee Discount',
  63.                 'desc' => 'Some type of surcharge description',
  64.                 'type' => '%', //percentage discount of total price after taxes i.e. %3 extra
  65.                 'amount' => '3.00',
  66.                 'tax' => 'pre' //discount applied BEFORE taxes have been added, and IS taxable
  67.             );
  68.             //This oadds a $10 discount after taxes have been applied
  69.             $EM_Booking->booking_meta['discounts']['super_special'] = array(
  70.                 'name' => 'Super Special Discount',
  71.                 'desc' => 'Some type of discount description',
  72.                 'type' => '#', //numerical discount i.e. $10.00 off
  73.                 'amount' => '10.00',
  74.                 'tax' => 'post' //discount applied AFTER taxes have been added, and IS NOT taxable
  75.             );
  76.         }
  77.         $EM_Booking->calculate_price();
  78.         return $result;
  79.     }
  80.    
  81.     /**
  82.      * Example function for handling discounts and surcharges within Multiple Bookings. Adjustments are made (and can currently only be made) to the multiple booking object, not individual bookings.
  83.      * @param EM_Multiple_Booking Multiple Booking object, containing individual bookings within.
  84.      * @param mixed $booking_data - Supplied information to create EM_Multiple_Booking object.
  85.      * @return bool
  86.      * @see EM_Price_Adjustments::add_price_adjustments()
  87.      */
  88.     public static function add_mb_price_adjustments( $event_id, $EM_Booking, $EM_Multiple_Booking ){
  89.         //$bookings_data must be empty here, because otherwise it means this booking object already existed at one point.
  90.         return static::add_price_adjustments(true, $EM_Multiple_Booking);
  91.     }
  92.    
  93.     /**
  94.      * Adds price adjustments to default pre-selected tickets, which is reflected on page load in booking summary.
  95.      * @param EM_Booking $EM_Booking
  96.      * @return EM_Booking
  97.      * @see EM_Price_Adjustments::add_price_adjustments()
  98.      */
  99.     public static function get_intent_default( $EM_Booking ){
  100.         if( $EM_Booking ) {
  101.             static::add_price_adjustments( true, $EM_Booking );
  102.             return $EM_Booking;
  103.         }
  104.         return $EM_Booking;
  105.     }
  106. }
  107. EM_Price_Adjustments::init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement