Add to cart bookable product by URL - WooCommerce Bookings

喜夏-厌秋 提交于 2019-12-12 09:01:30

问题


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


回答1:


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>


来源:https://stackoverflow.com/questions/40801481/add-to-cart-bookable-product-by-url-woocommerce-bookings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!