Advertisement
eventsmanager

Adding Donation or Surcharge to Payment Gateways

Aug 8th, 2019 (edited)
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.70 KB | None | 0 0
  1. <?php
  2. /*
  3. 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.
  4.  
  5. Instructions:
  6.  
  7. 1. in your Forms Editor, create a new field
  8. – label => Donation Amount (or whatever you wish)
  9. – field id => donation_amount (or what matches the $donation_field_name property value below)
  10. – type => select or text
  11.     for text fields you could us a regex like this : ^[0-9]+(\.[+0-9]{2})?$
  12.     for select boxes under option you can add the amount per line, which must be numeric
  13.  
  14. 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/
  15.  
  16.  */
  17. class EM_Custom_Donaation {
  18.    
  19.     /**
  20.      * @var string Change this value to match the field id for donations in your booking form.
  21.      */
  22.     static $donation_field_name = 'donation_amount';
  23.     /**
  24.      * @var string This will be what appears in the booking summaries, you can change this to whatever you want.
  25.      */
  26.     static $label = 'Donation';
  27.    
  28.     /**
  29.      * Initialises the class and adds the necessary hooks.
  30.      * @return void
  31.      */
  32.     public static function init() {
  33.         add_filter('em_booking_get_post', array( static::class, 'donation_adjustment' ), 100, 2);
  34.         add_action( 'em_booking_form_confirm_footer', array( static::class, 'js_intent'), 10 );
  35.         // uncomment here if you want to give a default donation amount, but bear in mind this should match that in the text field
  36.         //add_filter('em_bookings_get_intent_default', array( static::class, 'intent_default' ), 100);
  37.     }
  38.    
  39.     /**
  40.      * Adds a surcharge if the donation field is filled in with a number.
  41.      * @param $result
  42.      * @param $EM_Booking
  43.      *
  44.      * @return mixed
  45.      */
  46.     public static function donation_adjustment( $result, $EM_Booking ){
  47.         if( $result ){
  48.            
  49.             if( empty($EM_Booking->booking_meta['surcharges']) ) $EM_Booking->booking_meta['surcharges'] = array();
  50.            
  51.             //donation_amount = Field Id from Events > Forms Editor
  52.             if ( !empty($EM_Booking->booking_meta['booking'][static::$donation_field_name]) && is_numeric($EM_Booking->booking_meta['booking'][static::$donation_field_name]) ) {
  53.                 $amount = $EM_Booking->booking_meta['booking'][static::$donation_field_name];
  54.            
  55.                 $EM_Booking->booking_meta['surcharges'][] = array(
  56.                     'name' => static::$label,
  57.                     //'desc' => 'Some type of surcharge description',
  58.                     'type' => '#', //numerical discount i.e. $10.00 off
  59.                     'amount' => $amount,
  60.                     'tax' => 'pre' //discount applied BEFORE taxes have been added, and IS taxable
  61.                 );
  62.             };
  63.            
  64.         }
  65.         return $result;
  66.     }
  67.    
  68.     /**
  69.      * Uncomment the line in init() to use this function.
  70.      * @param $EM_Booking
  71.      *
  72.      * @return mixed
  73.      */
  74.     public static function intent_default ( $EM_Booking ){
  75.         if( $EM_Booking ) {
  76.             // 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
  77.             static::donation_adjustment( true, $EM_Booking );
  78.         }
  79.         return $EM_Booking;
  80.     }
  81.    
  82.     /**
  83.      * Outputs JS that will update the booking summary when the donation field is changed.
  84.      * @return void
  85.      */
  86.     public static function js_intent() {
  87.         ?>
  88.         <script type="text/javascript">
  89.             document.addEventListener("em_booking_form_init", function( e ) {
  90.                 let booking_form = e.target;
  91.                 booking_form.addEventListener("change", function( e ) {
  92.                     if( e.target.matches('input[name="donation_amount"]') ){
  93.                         booking_form.dispatchEvent( new CustomEvent('em_booking_form_updated') );
  94.                     }
  95.                 });
  96.             });
  97.         </script>
  98.         <?php
  99.     }
  100. }
  101. EM_Custom_Donaation::init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement