2017-07-09 111 views
0

我需要爲任何woocommerce產品創建單個變體,但每次點擊保存時都會創建另一個變體。 我是來自functions.php的爲woocommerce產品創建單個變體

工作循環之外,爲什麼每次我打保存它創建另一個變化中,需要幫助創建僅1變化/小孩後

add_action (save_post, create_new_vars); 
function create_new_vars ($post) { 
global $post; 
$children = get_pages('child_of='.$post->ID); 
if(count($children) >= 1) {  
return; 
} 

$my_post = array(
    'post_title' => 'Color for #' . $post->ID, 
    'post_name' => get_the_title($post->ID), 
    'post_status' => 'publish', 
    'post_parent' => $post->ID, 
    'post_type' => 'product_variation', 
    'guid' => home_url() . '/?product_variation=' . get_the_title($post->ID) 
); 

    remove_action('save_post', __FUNCTION__); 
    $attID = wp_insert_post($my_post); 

} 

回答

0

確定的答案是

add_action (save_post, create_new_vars); 
function create_new_vars ($post) { 
global $post; 
$args = array(
    'post_title' => 'Color for #' . $post->ID, 
    'post_name' => get_the_title($post->ID), 
    'post_status' => 'publish', 
    'post_parent' => $post->ID, 
    'post_type' => 'product_variation', 
    'guid' => home_url() . '/?product_variation=' . get_the_title($post->ID)  
); 
$children = get_children($args); 
if(count($children) < 1) {  


$my_post = array(
    'post_title' => 'Color for #' . $post->ID, 
    'post_name' => get_the_title($post->ID), 
    'post_status' => 'publish', 
    'post_parent' => $post->ID, 
    'post_type' => 'product_variation', 
    'guid' => home_url() . '/?product_variation=' . get_the_title($post->ID) 
); 

    remove_action('save_post', __FUNCTION__); 
    $attID = wp_insert_post($my_post); 

} 
}