回答

0

最近我遇到了同樣的問題,但我發現了一個解決方案。

這是不可能僅僅通過添加合適的變量到URL的末尾添加一個可預訂的產品,因爲WooCommerce預訂預計通過POST方法要發送的數據,你可以在插件的源代碼看到:

// 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); 

和:

// 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'], 
    ); 

取而代之的是:

<a href="/?add-to-cart=product_id">Add to cart</a> 

您需要使用「添加到購物車」按鈕/鏈接作爲提交按鈕來創建一個包含多個隱藏字段的表單。

事情是這樣的:

<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> 
相關問題