![](/img/trans.png)
[英]Add person types programmatically on bookable product creation in Woocommerce 3
[英]How to add a bookable product to woocommerce cart programmatically?
我正在嘗試以編程方式添加可預訂的產品,最后有些掙扎。
我使用的是自定義預訂詳細信息頁面,我希望客戶直接從該頁面進行預訂。 我的方法從這里開始: 通過URL添加到購物車可預訂產品-WooCommerce Bookings
該產品確實已使用列出的代碼添加到購物車中,但是我確實需要添加元數據,因為購物車顯示錯誤,因此持續時間丟失,並且不能為0。
<form class="cart" method="post" enctype="multipart/form-data">
<input type="hidden" name="add-to-cart" value="product_id" class="wc-booking-product-id" />
<input type="hidden" name="start-date" value="start_date">
<input type="hidden" name="end-date" value="end_date">
<input type="hidden" name="persons" value="1">
<input type="submit" name="book" class="check_btn" value="Buy">';
</form>
與此PHP:
if(isset($_POST['book'])){
global $woocommerce;
$woocommerce->cart->add_to_cart( $product_id );
}
do_action( 'woocommerce_after_add_to_cart_form' );
我確信對此有更好的解決方案,因為這似乎是一種解決方法,可能會留下其他問題。 另外,我想在將商品添加到購物車之前向商品添加更多元數據。
為了將產品放入購物車,我必須對表格進行一些更改,如下所示:
<form class="cart" method="post" enctype="multipart/form-data">
<input type="hidden" name="add-to-cart" value="'.$product_id.'" class="wc-booking-product-id" />
<input type="hidden" name="wc_bookings_field_duration" value="'.$lesson_days.'">
<input type="hidden" name="wc_bookings_field_persons" value="'.$participant_nums.'">
<input type="hidden" name="wc_bookings_field_start_date_day" value="'.$lesson_start_date->format('d').'">
<input type="hidden" name="wc_bookings_field_start_date_month" value="'.$lesson_start_date->format('m').'">
<input type="hidden" name="wc_bookings_field_start_date_year" value="'.$lesson_start_date->format('Y').'">
<input type="hidden" name="wc_bookings_field_start_date_time" value="'.$start_time.'">
<input type="submit" class="wc-bookings-booking-form-button single_add_to_cart_button button alt" value="Buy">
主要的困難是要知道開始日期分別需要年,月,日和時間。
一件事仍然困擾着我,根據一些內部計算來設置購物車價格還行不通。
要更新購物車中的價格,您需要首先刪除計算預訂/產品成本的操作。 這是一個ajax調用,因此您需要在2個地方將其刪除。
remove_action( 'wp_ajax_wc_bookings_calculate_costs', 'calculate_costs' );
remove_action( 'wp_ajax_nopriv_wc_bookings_calculate_costs', 'calculate_costs' );
完成此操作后,您可以添加自己的操作,以使用自己的計算進行更新。
如 -
add_action( 'wp_ajax_wc_bookings_calculate_costs', 'calculate_costs_mynewcosts', 5 );
add_action( 'wp_ajax_nopriv_wc_bookings_calculate_costs', 'calculate_costs_mynewcosts', 5 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.