问题描述
我在网站上有一个表单,允许用户创建可预订的产品.但是,我找不到如何使用 Woocommerce php 函数创建可用性间隔.有人有想法吗?
I have a form on a website that allows users to create bookable products. However, I can't find how to create a availability interval using Woocommerce php functions. Does anyone have an idea?
这是我如何创建我的产品
Here's how I create my product
$post_id = wp_insert_post( array(
'post_title' => $_POST["title"],
'post_content' => $_POST["description"],
'post_status' => 'publish',
'post_type' => "product",
) );
wp_set_object_terms( $post_id, 'booking', 'product_type' );
在同一个项目的另一部分中,我使用 get_post_meta 函数来获取可用性.我知道可用性是来自帖子的元数据,但我不确定如何更改这些.我尝试使用通常的 add_post_meta( $post_id, '_wc_booking_availability', $availability );
但这不起作用.我对 Woocommerce 的可用性有什么遗漏吗?
In another part of this same project, I use the get_post_meta function to get the availabilities. I get that the availabilities are meta data from the post but I'm not exacly sure how to change these. I tried using the usual add_post_meta( $post_id, '_wc_booking_availability', $availability );
but that didn't work. Is there something I'm missing about Woocommerce availabilities?
顺便提一下,是否有更详细的 Woocommerce Booking 文档?
On a side note, is there a more detailed documentation of Woocommerce Booking?
开发人员文档 在他们的网站上只涵盖了以编程方式创建产品的基础知识.
The developer documentation on their website only covers the basics of creating a product programmatically.
推荐答案
如果其他人需要,我找到了答案.
I found an answer if anyone else needs it.
//This is the array that will contain all the availabilities
$availabilities = array();
//Create an array that contains the required fields (from_date and to_date are not necessary in some types of availabilities)
$availability = array(
'type' => 'time:range', //Replace time:range with the type you need
'bookable' => 'yes',
'priority' => 10,
'from' => wc_booking_sanitize_time( "01:00" ),
'to' => wc_booking_sanitize_time( "03:00" ),
'from_date' => wc_clean( "2018-10-02" ),
'to_date' => wc_clean( "2018-10-02" )
);
//If you need to add more than one availability, make a loop and push them all into an array
array_push($availabilities, $availability);
add_post_meta( $post_id, '_wc_booking_availability', $availabilities );
我在这里找到了它,但我根据需要修改了它https://wordpress.stackexchange.com/questions/233904/how-to-add-date-range-in-woocommerce-with-code
I found it here but I modified it for my needshttps://wordpress.stackexchange.com/questions/233904/how-to-add-date-range-in-woocommerce-with-code
这篇关于以编程方式向 Woocommerce Bookings 上的可预订产品添加可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!