I'm using Woocommerce Bookings
How to add to cart bookable product by URL?
/?add-to-cart=[n.product]
How can I pass an avaiable date with variations?
Thanks
I'm using Woocommerce Bookings
How to add to cart bookable product by URL?
/?add-to-cart=[n.product]
How can I pass an avaiable date with variations?
Thanks
I ran into the same problem recently but I discovered a solution.
It's not possible to add a bookable product just by adding the right variables to the end of the URL because WooCommerce Bookings expects data to be sent via the POST
method as you can see at the plugin's source:
// woocommerce-bookings/includes/class-wc-booking-cart-manager.php : ln 256
$booking_form = new WC_Booking_Form( $product );
$cart_item_meta['booking'] = $booking_form->get_posted_data( $_POST );
$cart_item_meta['booking']['_cost'] = $booking_form->calculate_booking_cost( $_POST );
// Create the new booking
$new_booking = $this->create_booking_from_cart_data( $cart_item_meta, $product_id );
and:
// woocommerce-bookings/includes/class-wc-booking-cart-manager.php : ln 276
private function create_booking_from_cart_data( $cart_item_meta, $product_id, $status = 'in-cart' ) {
// Create the new booking
$new_booking_data = array(
'product_id' => $product_id, // Booking ID
'cost' => $cart_item_meta['booking']['_cost'], // Cost of this booking
'start_date' => $cart_item_meta['booking']['_start_date'],
'end_date' => $cart_item_meta['booking']['_end_date'],
'all_day' => $cart_item_meta['booking']['_all_day'],
);
Instead of this:
<a href="/?add-to-cart=product_id">Add to cart</a>
You need to create a form with a number of hidden fields with the 'Add to cart' button/link as the submit button.
Something like this:
<form method="post" action="/">
<input type="hidden" name="add-to-cart" value="product_id">
<input type="hidden" name="wc_bookings_field_start_date_year" value="booking_start_date_year">
<input type="hidden" name="wc_bookings_field_start_date_month" value="booking_start_date_month">
<input type="hidden" name="wc_bookings_field_start_date_day" value="booking_start_date_day">
<button type="submit">Add to cart</button>
</form>