Compatibility between Bookings and Gravity Forms Product Add-on extensions for WooCommerce

By October 31, 2016Tutorials

Recently we needed to create a booking system with add-on options and we decided to use WooCommerce, Bookings and Gravity Forms Product Add-Ons now knowing that they were not compatible. After encountering the problem, we fixed it, as it suits our needs. That is, we only need to make a single booking at a time (previously we modified the cart to remove any items inside, upon making a new booking).

The plugins used were the following versions:

  • WooCommerce 2.6.2
  • Gravity Forms 2.0.2
  • WooCommerce – Gravity Forms Product Add-Ons 2.10.10
  • WooCommerce Bookings 1.9.7

There is an issue with the specified versions of the plugins for WooCommerce of Gravity Forms Product Add-Ons and Bookings.
The issue is that selecting add-ons does not affect the price that the bookins plugin calculated. If one shows Gravity Forms’ calculations he will see that they work, but the Bookings plugin price is calculated separately. Also, on the checkout screen the price is not affected by Gravity Forms.

Since Bookings does not take into account Gravity Forms’ calculations, we should make it do so. This happens by editing the following file in the plugin:

wp-content\plugins\woocommerce-bookings\includes\booking-form\class-wc-booking-form.php

There, we have to look for the method: calculate_booking_cost – this method calculates the price and is used by both the cart and the ajax communication. We need to get to the end of it and find this line:

return apply_filters( 'booking_form_calculated_booking_cost', $this->booking_cost, $this, $posted );

And replace it with this code:

$add_price=0;
if (isset($posted['gform_form_id'])) {

  $lead=array();
  foreach($posted as $key=>$p) {
    if (strpos($key,'input_')===0) {
      $newkey=str_replace('input_','',$key);
      $newkey=str_replace('_','.',$newkey);
      $lead[$newkey]=$p;
    }
  }
  $lead['id'] = uniqid() . time() . rand();
  $form_meta = RGFormsModel::get_form_meta($posted['gform_form_id']);
  $products=array();
  $gf_cart=new WC_GFPA_Cart();
  $products = $gf_cart->get_product_fields( $form_meta, $lead );
  if ( !empty( $products["products"] ) ) {
    foreach ( $products["products"] as $productq ) {
      $price = GFCommon::to_number( $productq["price"] );
      if ( is_array( rgar( $productq, "options" ) ) ) {
        foreach ( $productq["options"] as $option ) {
          $add_price += GFCommon::to_number( $option["price"] );
        }
      }
      $subtotal = floatval( $productq["quantity"] ) * $price;
      $add_price += $subtotal;
    }

    $add_price += floatval( $products["shipping"]["price"] );
  }

}

return apply_filters( 'booking_form_calculated_booking_cost', $this->booking_cost+$add_price, $this, $posted );

This will get the form that is inserted in the booking page, calculate the amount that is to be increased and then adds it to the total booking cost.

Please note that this patch is intended for a single booking only and has not been tested for multiple ones at the same time.

We hope that this helps out people out there.

  • skyler

    This doesn’t work, at least any more… This is exactly what I’m looking for but the price block flashes then goes away :/

    • Creatizmo

      Hello. What version of the plugins did you use ? Are they the same as in the article ?
      This was used two months ago and unless there were major changes in the plugins, it should still work.

  • Himani Lotia

    Is it possible to insert the code under functions.php file?

    • Stanimir Genov

      It can be done, but this piece of code is intended to go into the plugin itself, since there is no guarantee it will work with any other versions

      • Himani Lotia

        This piece of code works in the plugin. I am looking for to insert into functions.php file

  • Andres Rios

    Hello everybody. I need to develop a similar solution for a booking system with add-on options or product designer module. Can you give me ideas for a mix of plugins that can do that job??, maybe without integration problems (I know, it is impossible) or something similar to this example with Bookings and Gravity Forms ???.