简体   繁体   English

在Woocommerce 3中以编程方式在可预订的产品创建中添加人员类型

[英]Add person types programmatically on bookable product creation in Woocommerce 3

Basically I'm trying to add a new bookable product in Woocommerce using a custom form I've made, so I have to add the products programmatically. 基本上我正在尝试使用我制作的自定义表单在Woocommerce中添加新的可预订产品,因此我必须以编程方式添加产品。 Creating the product is fine, the problem is that I can't figure out how to add a new person type to my product so I can fix multiple prices. 创建产品很好,问题是我无法弄清楚如何向我的产品添加新的人员类型,所以我可以修复多个价格。

Does someone have an idea how to do this? 有人知道怎么做吗?

This is the code I have so far. 这是我到目前为止的代码。

$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' );

The following will enable "has persons" and create your person types for a bookable product: 以下内容将启用“有人”并为可预订的产品创建您的人员类型:

// Create the product
$product_id = wp_insert_post( array(
    'post_title' => $_POST["title"],
    'post_content' => $_POST["description"],
    'post_status' => 'publish',
    'post_type' => "product",
) );

// Set the product type
wp_set_object_terms( $product_id, 'booking', 'product_type' );

// Get an instance of the WC_Product Object
$product = wc_get_product($product_id);

// Enable persons and save
$product->set_has_persons(true);
$product->save();

// Here define your person types (one array by person type)
$persons_type_data =  array(
    // First person type
    array( 
        'block_cost'  => 0,
        'cost'        => 16.50,
        'description' => '',
        'max'         => '',
        'min'         => '',
        'name'        => __('Adults'),
    ),
    // Second person type
    array(
        'block_cost'  => 0,
        'cost'        => 9.20,
        'description' => '',
        'max'         => '',
        'min'         => '',
        'name'        => __('Childs'),
    ),
);

// Loop Through persons type data
foreach( $persons_type_data as $key => $values ){
    $person_type = new WC_Product_Booking_Person_Type();
    $person_type->set_block_cost($values['block_cost']);
    $person_type->set_cost($values['cost']);
    $person_type->set_description($values['description']);
    $person_type->set_max($values['max']);
    $person_type->set_min($values['min']);
    $person_type->set_name($values['name']);
    $person_type->set_parent_id($product_id);
    $person_type->set_sort_order($key); // Sorting is based on the array order

    // Save the person type
    $person_type->save();

    // Add the person type to the array
    $persons[] = $person_type;
}
// Set person types and save the product
$product->set_person_types($persons);
$product->save();

Tested and works. 经过测试和工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式将可预订产品添加到woocommerce购物车? - How to add a bookable product to woocommerce cart programmatically? 将Woocommerce Bookings插件的“可用性(添加范围)”功能添加到前端以创建可预订的产品表单(Wordpress) - add Woocommerce Bookings plugin's Availability (Add Range) feature to frontend create bookable product form (Wordpress) 在 Woocommerce Bookings 中以编程方式设置可预订的产品基本成本 - Set bookable product base cost programatically in Woocommerce Bookings 如何以编程方式向 WooCommerce 产品添加类别? - How to add a category programmatically to a WooCommerce product? 以编程方式添加/更新 woocommerce 产品的 SEO 元关键字 - Add/update SEO metakeywords programmatically of woocommerce product 在 Woocommerce 中以编程方式添加带有评级的产品评论 - Add a product review with ratings programmatically in Woocommerce WooCommerce 根据日期创建以编程方式从类别中删除产品 - WooCommerce remove product from a category programmatically based on date creation 以编程方式在 Woocommerce 可下载产品中添加下载 - Add downloads in Woocommerce downloadable product programmatically 在Woocommerce中以编程方式添加新的产品类别 - Add new product categories programmatically in Woocommerce 在 WooCommerce 4 中以编程方式从自定义产品类型添加新产品 - Add a new product from a custom product type programmatically in WooCommerce 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM