Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- This snippet will allow you to add a donation text field to your Events Manager Pro booking form, which will then add that amount to the total payment when redirected to PayPal, Stripe or Offline Payment Gateways.
- Instructions:
- 1. in your Forms Editor, create a new field
- – label => Donation Amount (or whatever you wish)
- – field id => donation_amount (or what matches the $donation_field_name property value below)
- – type => select or text
- for text fields you could us a regex like this : ^[0-9]+(\.[+0-9]{2})?$
- for select boxes under option you can add the amount per line, which must be numeric
- 2. In your theme functions.php; paste the following snippet or for complete guide you can visit http://wp-events-plugin.com/tutorials/how-to-safely-add-php-code-to-wordpress/
- */
- class EM_Custom_Donaation {
- /**
- * @var string Change this value to match the field id for donations in your booking form.
- */
- static $donation_field_name = 'donation_amount';
- /**
- * @var string This will be what appears in the booking summaries, you can change this to whatever you want.
- */
- static $label = 'Donation';
- /**
- * Initialises the class and adds the necessary hooks.
- * @return void
- */
- public static function init() {
- add_filter('em_booking_get_post', array( static::class, 'donation_adjustment' ), 100, 2);
- add_action( 'em_booking_form_confirm_footer', array( static::class, 'js_intent'), 10 );
- // uncomment here if you want to give a default donation amount, but bear in mind this should match that in the text field
- //add_filter('em_bookings_get_intent_default', array( static::class, 'intent_default' ), 100);
- }
- /**
- * Adds a surcharge if the donation field is filled in with a number.
- * @param $result
- * @param $EM_Booking
- *
- * @return mixed
- */
- public static function donation_adjustment( $result, $EM_Booking ){
- if( $result ){
- if( empty($EM_Booking->booking_meta['surcharges']) ) $EM_Booking->booking_meta['surcharges'] = array();
- //donation_amount = Field Id from Events > Forms Editor
- if ( !empty($EM_Booking->booking_meta['booking'][static::$donation_field_name]) && is_numeric($EM_Booking->booking_meta['booking'][static::$donation_field_name]) ) {
- $amount = $EM_Booking->booking_meta['booking'][static::$donation_field_name];
- $EM_Booking->booking_meta['surcharges'][] = array(
- 'name' => static::$label,
- //'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
- );
- };
- }
- return $result;
- }
- /**
- * Uncomment the line in init() to use this function.
- * @param $EM_Booking
- *
- * @return mixed
- */
- public static function intent_default ( $EM_Booking ){
- if( $EM_Booking ) {
- // you may want to add something here to add default donation info, but also remember that the donation amounht must match the default value in the custom booking form field
- static::donation_adjustment( true, $EM_Booking );
- }
- return $EM_Booking;
- }
- /**
- * 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('input[name="donation_amount"]') ){
- booking_form.dispatchEvent( new CustomEvent('em_booking_form_updated') );
- }
- });
- });
- </script>
- <?php
- }
- }
- EM_Custom_Donaation::init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement