Advertisement
eventsmanager

Variable Surcharges based on spaces/hrs and booking form field

Apr 9th, 2024
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.80 KB | None | 0 0
  1. <?php
  2. /**
  3.  * This class will handle variable surcharges based on event duration and extra charges based on form field.
  4.  *
  5.  * The first charge is based on number of spaces booked and hours of the event, this will automatically be applied to all events, modify $variant and $variant name to change the surcharge price and name.
  6.  *
  7.  * The second charge will come into effect if you create a custom booking form field with the id 'extra_charge' (or the name of $extra_field_id if you modify it) and will add a fixed amount to the booking.
  8.  * If the field is a dropdown or anything that has a numeric value, that will be multipled by the $extra_cost field. If a checkbox, then $extra_cost will be applied.
  9.  *
  10.  * To install, download the contents of this snippet into a file with .php extension, and save it to your /wp-content/mu-plugins folder (create if necessary).
  11.  *
  12.  */
  13. class EM_Variable_Surcharges {
  14.    
  15.     // This is a variable fee based on number of spaces x hr x variant
  16.     public static $variant = 10;
  17.     public static $variant_name = "Fuel Surcharge";
  18.     // Uncomment the line below and the lines further down starting with 'desc' to add a description to the extra charge
  19.     //public static $variant_description = "Price based on €10 per hour.";
  20.    
  21.     // This is for the extra fee based on form field
  22.     public static $extra_field_id = 'extra_charge';
  23.     public static $extra_cost = 10;
  24.     public static $extra_name = 'Extra Charge';
  25.     // Uncomment the line below and the lines further down starting with 'desc' to add a description to the extra charge
  26.     //public static $extra_description = 'This is an extra charge description.';
  27.    
  28.     public static function init() {
  29.         add_action('em_multiple_booking', array( static::class, 'em_mb_add_price_adjustments'), 10, 2);
  30.         add_filter('em_booking_get_post', array( static::class, 'em_add_price_adjustments'), 100, 2);
  31.         add_filter('em_bookings_get_intent_default', array( static::class, 'em_bookings_get_intent_default'), 10);
  32.         add_action( 'em_booking_form_confirm_footer', array( static::class, 'js_intent'), 10 );
  33.     }
  34.    
  35.     /**
  36.      * @param bool $result
  37.      * @param EM_Booking $EM_Booking
  38.      *
  39.      * @return mixed
  40.      */
  41.     public static function em_add_price_adjustments( $result, $EM_Booking ){
  42.         //Only apply surcharge if booking has passed validation and therefore can be saved to database
  43.         //You could also do further checks here if you want to give discounts to specific events or bookings
  44.         if( $result ){
  45.             //Ensure we have arrays assigned to booking meta, if not create them to avoid PHP warnings
  46.             if( empty($EM_Booking->booking_meta['surcharges']) ) $EM_Booking->booking_meta['surcharges'] = array();
  47.            
  48.             // calculate hrs of event, then add surcharge based on that
  49.             $EM_Event = $EM_Booking->get_event();
  50.             $duration_seconds = $EM_Event->end()->getTimestamp() - $EM_Event->start()->getTimestamp();
  51.             $duration_hours = $duration_seconds / 3600;
  52.             $spaces = $EM_Booking->get_spaces();
  53.            
  54.             //This one adds a fixed $25 surcharge and is applied before taxes
  55.             $EM_Booking->booking_meta['surcharges'][] = array(
  56.                 'name' => static::$variant_name,
  57.                 //'desc' => static::$variant_description,
  58.                 'type' => '#', //numerical discount i.e. $10.00 off
  59.                 'amount' => $spaces * ($duration_hours * static::$variant),
  60.                 'tax' => 'pre' //discount applied BEFORE taxes have been added, and IS taxable
  61.             );
  62.            
  63.             // Extra Fee = Field Id from Events > Forms Editor
  64.             if ( !empty($EM_Booking->booking_meta['booking'][static::$extra_field_id]) ) {
  65.                
  66.                 if ( is_numeric($EM_Booking->booking_meta['booking'][static::$extra_field_id]) ) {
  67.                     $qty = $EM_Booking->booking_meta['booking'][static::$extra_field_id];
  68.                     $amount = $qty * static::$extra_cost;
  69.                 } else {
  70.                     $amount = static::$extra_cost;
  71.                 }
  72.                
  73.                 $EM_Booking->booking_meta['surcharges'][] = array(
  74.                     'name' => static::$extra_name,
  75.                     //'desc' => 'Some type of surcharge description',
  76.                     'type' => '#', //numerical discount i.e. $10.00 off
  77.                     'amount' => $amount,
  78.                     'tax' => 'pre' //discount applied BEFORE taxes have been added, and IS taxable
  79.                 );
  80.             };
  81.         }
  82.         $EM_Booking->calculate_price();
  83.         return $result;
  84.     }
  85.    
  86.     public static function em_bookings_get_intent_default( $EM_Booking ){
  87.         if( $EM_Booking ) {
  88.             static::em_add_price_adjustments( true, $EM_Booking );
  89.             return $EM_Booking;
  90.         }
  91.         return $EM_Booking;
  92.     }
  93.    
  94.     /**
  95.      * 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.
  96.      * @param EM_Multiple_Booking Multiple Booking object, containing individual bookings within.
  97.      * @param mixed $booking_data - Supplied information to create EM_Multiple_Booking object.
  98.      */
  99.     public static function em_mb_add_price_adjustments( $EM_Multiple_Booking, $booking_data ){
  100.         //$bookings_data must be empty here, because otherwise it means this booking object already existed at one point.
  101.         //If booking already existed, we'd either apply adjustments multiple times before checkout, or apply to an existing booking when modified which is not correct either.
  102.         if( !$booking_data ){
  103.             static::em_add_price_adjustments(false, $EM_Multiple_Booking, true);
  104.         }
  105.     }
  106.    
  107.     /**
  108.      * Outputs JS that will update the booking summary when the donation field is changed.
  109.      * @return void
  110.      */
  111.     public static function js_intent() {
  112.         ?>
  113.         <script type="text/javascript">
  114.             document.addEventListener("em_booking_form_init", function( e ) {
  115.                 let booking_form = e.target;
  116.                 booking_form.addEventListener("change", function( e ) {
  117.                     if( e.target.matches('[name="<?php echo esc_js(static::$extra_field_id); ?>"]') ){
  118.                         booking_form.dispatchEvent( new CustomEvent('em_booking_form_updated') );
  119.                     }
  120.                 });
  121.             });
  122.         </script>
  123.         <?php
  124.     }
  125. }
  126. EM_Variable_Surcharges::init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement