简体   繁体   English

如何以编程方式将可预订产品添加到woocommerce购物车?

[英]How to add a bookable product to woocommerce cart programmatically?

I am trying to add a bookable product programmatically and have some struggles in the end. 我正在尝试以编程方式添加可预订的产品,最后有些挣扎。

I am using a custom booking detail page from which I want customers to directly book. 我使用的是自定义预订详细信息页面,我希望客户直接从该页面进行预订。 My approach started out from this: Add to cart bookable product by URL - WooCommerce Bookings 我的方法从这里开始: 通过URL添加到购物车可预订产品-WooCommerce Bookings

The product does get added to the cart with the listed code, but I do need to add metadata, since the cart shows an error, that duration is missing and cannot be 0. 该产品确实已使用列出的代码添加到购物车中,但是我确实需要添加元数据,因为购物车显示错误,因此持续时间丢失,并且不能为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>

With this php: 与此PHP:

if(isset($_POST['book'])){ 
    global $woocommerce;
    $woocommerce->cart->add_to_cart( $product_id );
}
do_action( 'woocommerce_after_add_to_cart_form' ); 

I am sure there are better solutions to this, since this seems to be a workaround that might leave other issues. 我确信对此有更好的解决方案,因为这似乎是一种解决方法,可能会留下其他问题。 Additionally I'd like to add some more metadata to the item before adding it to the cart. 另外,我想在将商品添加到购物车之前向商品添加更多元数据。

I had to change the form a bit in order to put the product into the cart as follows: 为了将产品放入购物车,我必须对表格进行一些更改,如下所示:

<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">

The main difficulty was knowing that the start date needs year, month, day, and time seperately. 主要的困难是要知道开始日期分别需要年,月,日和时间。

One thing is still bothering me, the setting the cart price according to some internal calculations does not work yet. 一件事仍然困扰着我,根据一些内部计算来设置购物车价格还行不通。

To update the price in the cart you will need to first remove the action that calculates the booking/products costs. 要更新购物车中的价格,您需要首先删除计算预订/产品成本的操作。 This is an ajax call so you need to remove it in 2 places. 这是一个ajax调用,因此您需要在2个地方将其删除。

remove_action( 'wp_ajax_wc_bookings_calculate_costs',  'calculate_costs' );
remove_action( 'wp_ajax_nopriv_wc_bookings_calculate_costs', 'calculate_costs'  );

Once you do that, you can add back your own actions for updating with your own calculations. 完成此操作后,您可以添加自己的操作,以使用自己的计算进行更新。

Such as - 如 -

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM