我使用以下代码为产品变体生成 id。
$i = 0;
while($i < 2){ // 2 is example
$product = wc_get_product($product_id);
$variation_post = array(
'post_title' => 'Variation #' . $i . ' of ' . $product->get_name(),
'post_name' => 'product-'.$product_id.'-variation',
'post_status' => 'publish',
'post_parent' => $product_id,
'post_type' => 'product_variation',
'guid' => $product->get_permalink()
);
if((post_exists('Variation #' . $i . ' of ' . $product->get_name())) == 0){
$variation_id = wp_insert_post( $variation_post );
}
$variation = new WC_Product_Variation( $variation_id );
$i++;
}
但wp_insert_post
生成两个 id 而不是一个。我在ajax文件中使用这段代码,问题出在哪里?!