如何将资源链接到WooCommerce上的可预订产品计划?

发布于 2025-01-22 09:18:55 字数 609 浏览 0 评论 0原文

我正在使用WooCommerce Bookings插件来处理我的业务预订。我希望能够以编程方式将资源添加到可预订的产品中。现在,我能够通过这样做来创建资源而没有任何问题:

$new_page_args = array(
    'post_type'     => 'bookable_resource',
    'post_title'    => $resource_name,
    'post_status'   => 'publish',
    'post_author'   => 1,
);
        
$resource_id = wp_insert_post($new_page_args);

但是我似乎找不到将新创建的资源与产品链接的方法。 我尝试执行此查询:

$wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->prefix}wc_booking_relationships ( product_id, resource_id ) VALUES ( %d, %d )", $product_id, $resource_id ) );

它仍然不起作用。

I'm using the Woocommerce Bookings plugin to handle bookings for my business. I would like to be able to add resources to bookable products programmatically. Right now I'm able to create resources without any problems by doing this:

$new_page_args = array(
    'post_type'     => 'bookable_resource',
    'post_title'    => $resource_name,
    'post_status'   => 'publish',
    'post_author'   => 1,
);
        
$resource_id = wp_insert_post($new_page_args);

However I can't seem to find a way to link the newly created resource with a product.
I have tried executing this query:

$wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->prefix}wc_booking_relationships ( product_id, resource_id ) VALUES ( %d, %d )", $product_id, $resource_id ) );

and it still doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫言歌 2025-01-29 09:18:55

这是我用来将旧预订链接到新资源的代码段。

$bookings = WC_Booking_Data_Store::get_booking_ids_by(
    array(
    'date_between' => array('start' => $dtStart->getTimestamp(),'end' => $dtEnd->getTimestamp()),
    'status' => array('paid','confirmed','complete'),
));
echo "Bookings: <pre>"; print_r($bookings); echo "</pre>";

foreach ( $bookings as $bookingId ) {
    $booking = new WC_Booking($bookingId);
    echo "Booking: <pre>"; print_r($booking); echo "</pre>";
    $resource = $booking->resource_id;
    $product = $booking->get_product();
    $p_title = $product->get_title();
    $output = sprintf(
    'Booking ID: %s Resource ID: %s Product ID: %s Product Name: %s',
    $bookingId,
    $resource,
    $product->id,
    $p_title
        );
    echo "Output: <pre>"; print_r($output); echo "</pre>";
    if ( $resource == 0) {
        if ( $product_id == 45288 ) { $new_resource_id =  78240 ; } else { $new_resource_id  =  77672 ; }
        $booking->update_meta_data( '_booking_resource_id',  $new_resource_id );
        $booking->save();
    }
}

它对我有用。

Here is a code snippet I used to link old bookings to new resources.

$bookings = WC_Booking_Data_Store::get_booking_ids_by(
    array(
    'date_between' => array('start' => $dtStart->getTimestamp(),'end' => $dtEnd->getTimestamp()),
    'status' => array('paid','confirmed','complete'),
));
echo "Bookings: <pre>"; print_r($bookings); echo "</pre>";

foreach ( $bookings as $bookingId ) {
    $booking = new WC_Booking($bookingId);
    echo "Booking: <pre>"; print_r($booking); echo "</pre>";
    $resource = $booking->resource_id;
    $product = $booking->get_product();
    $p_title = $product->get_title();
    $output = sprintf(
    'Booking ID: %s Resource ID: %s Product ID: %s Product Name: %s',
    $bookingId,
    $resource,
    $product->id,
    $p_title
        );
    echo "Output: <pre>"; print_r($output); echo "</pre>";
    if ( $resource == 0) {
        if ( $product_id == 45288 ) { $new_resource_id =  78240 ; } else { $new_resource_id  =  77672 ; }
        $booking->update_meta_data( '_booking_resource_id',  $new_resource_id );
        $booking->save();
    }
}

It worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文