Search
Close this search box.
Search

How to Add Custom Bookings Data in the WooCommerce Cart

Listen to this article

happy-developerIf I had a $ for every time a client asked me to customize the bookings plugin, I’d have enough money to buy the WooCommerce Bookings plugin 😀 . So I’m exaggerating a bit, but honestly, we have a lot of requests sent our way to customize WooCommerce Bookings. A common customization request is to add custom fields for a bookable product, the values of which have to be then added to the WooCommerce cart, and eventually the order.

So I thought why not write how to do this so that it can help those of you who are looking to do the same.

In this post, I won’t be covering how to add custom fields since I’ve covered it before in how to add custom settings in WooCommerce. Today, I’ll guide you through the process of adding custom data to WooCommerce cart for bookable products.

This post is different from a post we’d previously written about adding custom data to the WooCommerce Order. In this post, we’ll be focussing only on bookable products. The WooCommerce Bookings plugin provides its own fields. We’ll be using these fields to add custom data to the WooCommerce cart.

So let’s get straight to it.

[space]

It’s All About Finding the Right Hook and Using It

As is the case in WordPress development, it’s all about finding the right hook. In our case, we’re looking to change contents in the WooCommerce cart. So we’re looking for a WooCommerce Hook. And the specific hook we’re looking for is the ‘woocommerce_add_cart_item_data’.

The ‘woocommerce_add_cart_item_data’ is a filter that allows us to tweak contents in our WooCommerce cart. So to change the contents of the cart, we’ll have to hook your function on this filter and change the cart item meta.

If that’s enough information for you, you don’t need to read further. If not, read on.

[space]

How to Add WooCommerce Bookings Data in the Cart

To make things simpler to follow, let’s adopt an example. Let’s say we use the WooCommerce Bookings plugin to book a travel package. The custom data that we would need to add are the cities the visitor wants to travel to. Let’s say the cities are selected by the user and saved as user meta. We need to add this meta information as bookings data to the WooCommerce cart.

So, let’s hook on the ‘woocommerce_add_cart_item_data’ filter, and add our data. You can do this using a similar function like the one below:

// add the below code in a custom plugin or in functions.php of your theme.
add_filter( 'woocommerce_add_cart_item_data', array( $this, 'wdm_add_cities_to_cart' ), 11, 2 );

// the function accepts two parameters, the cart data and the product id
public function wdm_add_cities_to_cart( $cart_item_meta, $product_id ) {
    // let's consider that the user is logged in
    $user_id = get_current_user_id();
    if( 0 != $user_id)
    {
        // set the values as bookings meta
        $cart_item_meta['booking']['From'] = get_user_meta( $user_id, 'FROM', true );
        $cart_item_meta['booking']['To'] = get_user_meta( $user_id, 'TO', true );  
    }
    return $cart_item_meta;
}

All of this works well when we don’t have to add an additional cost. But let’s say, we need to provide a user an option to pick a hotel as well. In this case, adding an hotel might incur additional cost.

In this case, we have to alter the booking price. Let’s take a look at how this can be done.

[space]

How to Change Product Price in WooCommerce Bookings

Any guesses how this can be done? You’re right! You need a hook. And the filter you’re looking for is ‘booking_form_calculated_booking_cost’.

Now if we consider that the cost of booking each hotel is saved in the database, we’ll have to retrieve the cost and add it to the price of the bookable product. So the code has to be similar to the code snippet below:

add_filter( 'booking_form_calculated_booking_cost', array($this, 'wdm_add_hotel_booking'), 11, 3);
public function wdm_add_hotel_booking( $cost, $book_obj, $posted ) {
    $user_id = get_current_user_id();

    // check if user has picked a hotel
    if(( 0 != $user_id) && (true == get_user_meta( $user_id, 'HOTEL', true )))
    {
        // set hotel name
        $cart_item_meta['booking']['To'] = get_option( 'WDM_HOTEL_NAME' );

        // get the hotel price
        $custom_price = get_option('WDM_HOTEL_PRICE');
        $cost = $cost + $custom_price;
    }
    return $cost;
}

Once you’ve done this, you’ll notice the fields when you add a bookable product to the cart.

wc-cart-with-custom-bookings-data
WooCommerce Cart with Custom Bookings Data

These custom fields which have been added will show up on your checkout page as well.

wc-checkout-with-custom-bookings-data
WooCommerce Checkout Page with Custom Bookings Data

[space]

Hope this article has helped you modify bookings data in your WooCommerce cart. If you have any doubts or questions, you can ask them using the comment section and I’ll try my best to help you out!

Praveen Chauhan

Praveen Chauhan

12 Responses

  1. can we do like the booking widget is there and bellow that i have tot add 4-5 check boxes having diffrent price add on there click .
    help me to do it

    1. Hi Rohit,
      If I’ve understood your requirement correctly, you need to explore the Gravity Forms add-on for WooCommerce. Take a look at the extension, and check if that suits your needs.

  2. Hi,
    I try to use this hook (public function wdm_add_hotel_booking( $cost, $book_obj, $posted ) {…} ).

    Badly, I am not able to use $book_obj and $posted to adjest price – They are empty.

    Do you have an idea to recuperate its??

    Many thanks,

    public function wdm_add_hotel_booking( $cost, $book_obj, $posted ) {

    1. Hi Sylvain,

      Could you let me know the version of WooCommerce Bookings plugin you are using?

  3. i am using the formidable plugin for adding the custom data but i want to integrate the woocommerce to formidable plugin for adding the formidable form data into the woocommerce cart

    plz answer………..

  4. Hi how can i customize that the date picker of booking extension apply to all products in shop so client choose term and after it , choose all gear the want to rent, and each item already have the price for the term the picked before and then they add it to the cart….

  5. I’m not getting by using this method, I have many data’s to update for a single product, so I serialised the filtered data to update, but it is not updating as well, can you elaborate a bit on this.

  6. Hi, great article, exactly what I was looking for 🙂

    We are looking to add the booking product price ($9/a day) to each booking cart item.
    Like this:

    Booking Product Name
    $9 per day

    Thanks again for your excellent articles!
    Luke

  7. i want to make tour website and i want that the rate must contain transportation charge + hotel charge. how can i add air fare from any site like makemytrip.com

  8. Hello Praveen,

    I know this is a quite old post but Google brought me here while I was looking for some details. If I am not mistaken even in this example we are saving the custom details to ‘order’ instead of the ‘booking’ itself. I wonder if there is a way to add and save custom meta details to bookings itself so those are available by accessing the booking IDs? Let’s say for example, a booking note. Is it possible to do that as I couldn’t find any hooks to add custom meta to a booking itself.

    Thanks in advance,

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

WordPress Development

Custom WordPress Solutions, for You

LearnDash Development

Custom LearnDash Solutions, for You

WooCommerce Development

Custom WooCommerce Solutions, for You

WordPress Plugins

Scale your WordPress Business

WooCommerce Plugins

Scale your WooCommerce Business

LearnDash Plugins

Scale your LearnDash Business

Label

Title

Subtext

Label

Title

Subtext

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads