【问题标题】:Adding availabilities programmatically to a bookable product on Woocommerce Bookings以编程方式将可用性添加到 Woocommerce Bookings 上的可预订产品
【发布时间】:2018-10-10 13:48:23
【问题描述】:

我在网站上有一个表单,允许用户创建可预订的产品。但是,我找不到如何使用 Woocommerce php 函数创建可用性间隔。有人有想法吗?

这是我创建产品的方式

$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 可用性,我有什么遗漏吗?

顺便说一句,是否有更详细的 Woocommerce Booking 文档?

他们网站上的developer documentation 仅涵盖以编程方式创建产品的基础知识。

【问题讨论】:

    标签: php wordpress woocommerce woocommerce-bookings


    【解决方案1】:

    如果其他人需要,我找到了答案。

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

    【讨论】:

    • 谢谢...您还应该添加帮助您发布此答案的链接(如果是这样的话)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    相关资源
    最近更新 更多