如何使用php代码将woocommerce产品添加到自定义Product_type?

时间:2017-07-06 09:43:04

标签: php wordpress woocommerce woocommerce-rest-api

WooCommerce产品很容易创建,但产品已添加到 product_type:simple 。我想将产品添加到“custom product_type:test”

static function createTicket($postId, $productDetails) {
$id = wp_insert_post(array(
    'post_type' => 'product',
    'post_title' => $productDetails['title'],
    'post_content' => $productDetails['description'],
    'post_status' => get_post_status($postId),
        'max_value' => 1,
));
update_post_meta($id, '_price', $productDetails['price']);
update_post_meta($id, '_regular_price', $productDetails['price']);
update_post_meta($id, '_wpws_testID', $postId);
update_post_meta($id, '_sold_individually', "yes");
wp_set_object_terms( $id, 'test', 'product_type' );
return $id;
}

我添加了

  

wp_set_object_terms($ id,'test','product_type');

但没有任何作用

enter image description here

1 个答案:

答案 0 :(得分:1)

使用product_type_selector挂钩

std::string
  

修改

请查看以下链接,它可以为您提供更多帮助

  

修改

旧版WooCommerce的上述代码

使用此代码保存具有自定义产品类型的产品。它与WooCommerce 3. *合作 有关详细信息,请查看此https://gist.github.com/stirtingale/753ac6ddb076bd551c3261007c9c63a5

// add a product type
add_filter( 'product_type_selector', 'add_custom_product_type' );
function add_custom_product_type( $types ){
    $types[ 'test_custom_product_type' ] = __( 'test Product' );
    return $types;
}
相关问题