2

I've added a couple new fields to the date-picker.php template in WooCommerce Bookings and need to trigger the AJAX recalculation of the booking cost on the product page when these fields are updated.

Currently this recalculation is triggered when the standard fields in date-picker.php are updated. However, I will be using the additional fields to recalculate the duration and booking cost so I need this AJAX recalculation triggered when updates are made to the new fields as well.

How can I trigger the AJAX recalculation? I have tried simply triggering a change event on one of the standard fields, but that doesn't seem to work.

christian
  • 2,279
  • 4
  • 31
  • 42
  • Is it possible to share the code of the new fields? I am trying to do it but couldn't. I don't want to use a plugin like extra product options. I want to add it myself. Thanks. – Tuna Zenginbaş Jan 04 '20 at 09:12

1 Answers1

0

I had the same issue. If it helps someone in the future, heres my work around :

Go to /wp-content/plugins/woocommerce-bookings/templates/single-product/add-to-cart/booking.php This is the template for the single product booking form. You can then move one of the actions such as <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?> (or create your own action) within the <div id="wc-bookings-booking-form"/>. Now add a custom field or code to this action (I used wc-fields-factory). When you change a field value within the booking form the AJAX recalculation is triggered. You can hook into this trigger in your functions.php file (within your theme) to alter the cost based on the custom fields you have added like so :

add_filter( 'booking_form_calculated_booking_cost', 'my_booking_cost', 10, 3 );
function my_booking_cost( $booking_cost, $booking, $posted ) { 
/* use fields here to show a new booking cost*/
      $my_time = $posted[ <name of custom field> ]; //access field within booking form
     $booking_cost = 1; //whatever your calc is
     return $booking_cost;
}
  • Please note that this filter has been superseded by the `woocommerce_bookings_calculated_booking_cost` filter in newer versions of the WooCommerce Bookings plugin. – strarsis Jun 30 '20 at 22:17