Php 为woocommerce产品创建单一变体
我需要为任何woocommerce产品创建一个变体,但是每次单击save时,它都会创建另一个变体。 我在functions.php的循环之外工作 为什么每次我点击save就会创建另一个变体,需要帮助才能创建一个变体/子帖子Php 为woocommerce产品创建单一变体,php,arrays,woocommerce,parent-child,Php,Arrays,Woocommerce,Parent Child,我需要为任何woocommerce产品创建一个变体,但是每次单击save时,它都会创建另一个变体。 我在functions.php的循环之外工作 为什么每次我点击save就会创建另一个变体,需要帮助才能创建一个变体/子帖子 add_action (save_post, create_new_vars); function create_new_vars ($post) { global $post; $children = get_pages('child_of='.$post->ID);
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);
}
好的,答案是
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);
}
}
添加动作(保存帖子,创建新变量);
函数创建新变量($post){
全球$员额;
$args=数组(
“贴子标题”=>“贴子颜色”。$post->ID,
“post\u name”=>获取标题($post->ID),
“发布状态”=>“发布”,
'post_parent'=>$post->ID,
“后类型”=>“产品变化”,
“guid'=>home\u url()。/?产品变量=”。获取标题($post->ID)
);
$children=get_children($args);
如果(计数($children)<1){
$my_post=数组(
“贴子标题”=>“贴子颜色”。$post->ID,
“post\u name”=>获取标题($post->ID),
“发布状态”=>“发布”,
'post_parent'=>$post->ID,
“后类型”=>“产品变化”,
“guid'=>home\u url()。/?产品变量=”。获取标题($post->ID)
);
删除操作(“保存日志”、“函数”);
$attID=wp\u insert\u post($my\u post);
}
}