Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This class will handle variable surcharges based on event duration and extra charges based on form field.
- *
- * 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.
- *
- * 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.
- * 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.
- *
- * 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).
- *
- */
- class EM_Variable_Surcharges {
- // This is a variable fee based on number of spaces x hr x variant
- public static $variant = 10;
- public static $variant_name = "Fuel Surcharge";
- // Uncomment the line below and the lines further down starting with 'desc' to add a description to the extra charge
- //public static $variant_description = "Price based on €10 per hour.";
- // This is for the extra fee based on form field
- public static $extra_field_id = 'extra_charge';
- public static $extra_cost = 10;
- public static $extra_name = 'Extra Charge';
- // Uncomment the line below and the lines further down starting with 'desc' to add a description to the extra charge
- //public static $extra_description = 'This is an extra charge description.';
- public static function init() {
- add_action('em_multiple_booking', array( static::class, 'em_mb_add_price_adjustments'), 10, 2);
- add_filter('em_booking_get_post', array( static::class, 'em_add_price_adjustments'), 100, 2);
- add_filter('em_bookings_get_intent_default', array( static::class, 'em_bookings_get_intent_default'), 10);
- add_action( 'em_booking_form_confirm_footer', array( static::class, 'js_intent'), 10 );
- }
- /**
- * @param bool $result
- * @param EM_Booking $EM_Booking
- *
- * @return mixed
- */
- public static function em_add_price_adjustments( $result, $EM_Booking ){
- //Only apply surcharge if booking has passed validation and therefore can be saved to database
- //You could also do further checks here if you want to give discounts to specific events or bookings
- if( $result ){
- //Ensure we have arrays assigned to booking meta, if not create them to avoid PHP warnings
- if( empty($EM_Booking->booking_meta['surcharges']) ) $EM_Booking->booking_meta['surcharges'] = array();
- // calculate hrs of event, then add surcharge based on that
- $EM_Event = $EM_Booking->get_event();
- $duration_seconds = $EM_Event->end()->getTimestamp() - $EM_Event->start()->getTimestamp();
- $duration_hours = $duration_seconds / 3600;
- $spaces = $EM_Booking->get_spaces();
- //This one adds a fixed $25 surcharge and is applied before taxes
- $EM_Booking->booking_meta['surcharges'][] = array(
- 'name' => static::$variant_name,
- //'desc' => static::$variant_description,
- 'type' => '#', //numerical discount i.e. $10.00 off
- 'amount' => $spaces * ($duration_hours * static::$variant),
- 'tax' => 'pre' //discount applied BEFORE taxes have been added, and IS taxable
- );
- // Extra Fee = Field Id from Events > Forms Editor
- if ( !empty($EM_Booking->booking_meta['booking'][static::$extra_field_id]) ) {
- if ( is_numeric($EM_Booking->booking_meta['booking'][static::$extra_field_id]) ) {
- $qty = $EM_Booking->booking_meta['booking'][static::$extra_field_id];
- $amount = $qty * static::$extra_cost;
- } else {
- $amount = static::$extra_cost;
- }
- $EM_Booking->booking_meta['surcharges'][] = array(
- 'name' => static::$extra_name,
- //'desc' => 'Some type of surcharge description',
- 'type' => '#', //numerical discount i.e. $10.00 off
- 'amount' => $amount,
- 'tax' => 'pre' //discount applied BEFORE taxes have been added, and IS taxable
- );
- };
- }
- $EM_Booking->calculate_price();
- return $result;
- }
- public static function em_bookings_get_intent_default( $EM_Booking ){
- if( $EM_Booking ) {
- static::em_add_price_adjustments( true, $EM_Booking );
- return $EM_Booking;
- }
- return $EM_Booking;
- }
- /**
- * 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.
- * @param EM_Multiple_Booking Multiple Booking object, containing individual bookings within.
- * @param mixed $booking_data - Supplied information to create EM_Multiple_Booking object.
- */
- public static function em_mb_add_price_adjustments( $EM_Multiple_Booking, $booking_data ){
- //$bookings_data must be empty here, because otherwise it means this booking object already existed at one point.
- //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.
- if( !$booking_data ){
- static::em_add_price_adjustments(false, $EM_Multiple_Booking, true);
- }
- }
- /**
- * Outputs JS that will update the booking summary when the donation field is changed.
- * @return void
- */
- public static function js_intent() {
- ?>
- <script type="text/javascript">
- document.addEventListener("em_booking_form_init", function( e ) {
- let booking_form = e.target;
- booking_form.addEventListener("change", function( e ) {
- if( e.target.matches('[name="<?php echo esc_js(static::$extra_field_id); ?>"]') ){
- booking_form.dispatchEvent( new CustomEvent('em_booking_form_updated') );
- }
- });
- });
- </script>
- <?php
- }
- }
- EM_Variable_Surcharges::init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement