StonehengeCreations

EM - Show Subtotal in Single Bookings Booking Form

Dec 15th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.83 KB | None | 0 0
  1. <?php
  2. /********************
  3.     This snippet will enable a sub total in the Single Bookings Booking Form for Events Manager Pro.
  4. ********************/
  5. function stonehenge_em_single_booking_subtotal($EM_Ticket) {
  6.     $locale         = get_bloginfo('language');
  7.     $ticket_price   = $EM_Ticket->get_price(false);
  8.     $currency       = get_option('dbem_bookings_currency');
  9.     $taxRate        = get_option('dbem_bookings_tax');
  10.     $taxAdd         = get_option('dbem_bookings_tax_auto_add');
  11.     $taxName        = __('Tax', 'events-manager');
  12.  
  13.     // Create the Sub Total section on the Booking From.
  14.     ?>
  15.     <style>
  16.         /* Remove this style section if you DO want to show additional tax info, or leave it to hide. */
  17.         #subTax {display:none !important;}
  18.         #extraInfo {display:none !important;}
  19.     </style>
  20.  
  21.     <?php
  22.     if( get_option('dbem_bookings_tickets_single_form') ) {
  23.         echo '<tr><td><label>' . __('Sub Total', 'events-manager') . '</label></td>';
  24.         echo '<td colspan="2"><strong><span id="subTotal"></span></strong> <small><span id="subTax"></span></small></td></tr>';
  25.     } else {
  26.         echo '<p class="input-user-field em-subtotal"><label>' .  __('Sub Total', 'events-manager') . '</label>';
  27.         echo '<strong><span id="subTotal"></span></strong> <small><span id="subTax"></span></small></p>';
  28.     }
  29.     ?>
  30.  
  31.     <!-- This is where the real magic happens! -->
  32.     <script>
  33.     jQuery.noConflict();
  34.         // Add 0 to Spaces, making required more clear. It will also make the initial Subtotal 0.00.
  35.         jQuery(".em-ticket-select").prepend("<option value='0' selected='selected' disabled='disabled'>---</option>");
  36.  
  37.         Locale          = '<?php echo $locale; ?>';
  38.         TicketPrice     = <?php echo $ticket_price; ?>;
  39.         Currency        = '<?php echo $currency; ?>';
  40.         TaxRate         = <?php echo $taxRate; ?>;
  41.         TaxName         = '<?php echo $taxName; ?>';
  42.         addTax          = '<?php echo $taxAdd; ?>';
  43.         Spaces          = jQuery(".em-ticket-select option:selected").val();
  44.         Price           = (Spaces * TicketPrice).toFixed(2);
  45.  
  46.         // Process Tax added to Ticket Price?
  47.         if( addTax == '1' ) {
  48.             Tax = (Price / (100 + TaxRate) * TaxRate).toFixed(2);
  49.             inclTax = '(incl. ';
  50.         }
  51.         else {
  52.             Tax = (Price / 100 * TaxRate).toFixed(2);
  53.             inclTax = '(excl. ';
  54.         }
  55.  
  56.         formattedPrice  = Number(Price).toLocaleString(Locale, {style: 'currency', currency: Currency });
  57.         formattedTax    = Number(Tax).toLocaleString(Locale, {style: 'currency', currency: Currency });
  58.  
  59.         // Add Tax info to the default ticket price field.
  60.         jQuery(".ticket-price strong").html(Number(TicketPrice).toLocaleString(Locale, {style: 'currency', currency: Currency }));
  61.         jQuery(".ticket-price").append("<span id='extraInfo'> p.p. <small>" + inclTax + TaxRate + "% " + TaxName + ")</small></span>");
  62.  
  63.         // Display initial sub total amount on page load.
  64.         jQuery("#subTotal").html( formattedPrice );
  65.         jQuery("#subTax").html( inclTax + formattedTax + ' ' + TaxName + ')' );
  66.  
  67.         // Change the sub total according to the selected spaces.
  68.         jQuery(".em-ticket-select").change(function() {
  69.             var newSpaces   = jQuery(".em-ticket-select option:selected").val();
  70.             var newPrice    = (newSpaces * TicketPrice ).toFixed(2);
  71.             var newTax      = (newPrice / (100 + TaxRate) * TaxRate ).toFixed(2);
  72.             jQuery("#subTotal").html( Number(newPrice).toLocaleString(Locale, { style: 'currency', currency: Currency }) );
  73.             jQuery("#subTax").html(inclTax + Number(newTax).toLocaleString(Locale, { style: 'currency', currency: Currency })+' '+TaxName+')');
  74.         });
  75.  
  76.         // Optional: Add the required asterisk after the Spaces label. Comment out if needed.
  77.         jQuery("label[for=em_tickets]").append(' <span class="em-form-required">*</span>');
  78.  
  79.     (jQuery);
  80.     </script>
  81.     <?php
  82. }
  83. if( get_option('dbem_bookings_tickets_single_form') ) {
  84.     add_action('em_booking_form_tickets_loop_footer', 'stonehenge_em_single_booking_subtotal');
  85. } else {
  86.     add_action('em_booking_form_ticket_footer', 'stonehenge_em_single_booking_subtotal');
  87. }
Advertisement
Add Comment
Please, Sign In to add comment