If 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 :D . 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.

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

[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!