您想要创建Woocommerce产品,并且要做好这项工作,您不仅需要post对象和缩略图:您还需要分类法和自定义字段:我认为您的产品至少会有一个价格,不是吗?
一旦您想要批量创建产品,我想对所有产品使用相同的分类法和相同的自定义字段对您来说都是有益的(无论如何,手动更改总是可能的)。
我将提出一种方法,并给出相关代码,但只需注意not 已测试。
First of all, manually create one product: 这将是你的reference 产品
为它设定一切:价格、变化、分类。。。每件事同时设置缩略图,偏离路线。
现在只是take note of the post ID 本产品的。
第二步是创建一个函数,该函数可以循环浏览附件并创建帖子、分配缩略图、自定义字段和分类法。
建议:请注意,woocommerce使用一些自定义表来保存一些信息,我的方法会忽略这些表,因此您不会将有关可下载产品的信息/设置批量分配给帖子。如果您想从图像中创建可下载的产品,则必须在使用该功能之前对其进行修改。此外,一些WooCommerce插件也使用自定义表:在运行函数之前请注意这一点
function wpa_convert_images_to_products($ref_id = 0, $skip_images = array() ) {
   // following line ensure that present function is runned once
   if (  get_transient(\'convert_images_to_products_done\') ) return;
   //try to remove php limits in execution time an memory
   @set_time_limit (0);
   @ini_set(\'memory_limit\', -1);
   if ( ! post_type_exists( \'product\' ) || ! $ref_id )
       wp_die(\'Reference post id is not valid or product post type is not registered.\');
   $reference = get_post($ref_id);
   if ( ! $reference )
       wp_die(\'Given reference post id is not valid.\');
   $ref_thumb = get_post_thumbnail_id( $ref_id );
   if ( ! is_int($ref_thumb) ) $ref_thumb = null;
   // get reference attributes
   $product_vars = get_object_vars($reference);
   unset($product_vars[\'ID\']);
   unset($product_vars[\'post_date\']);
   unset($product_vars[\'post_date_gmt\']);
   unset($product_vars[\'post_modified\']);
   unset($product_vars[\'post_modified_gmt\']);
   unset($product_vars[\'comment_count\']);       
   // get reference custom fields
   $ref_fields = get_post_custom( $ref_id );
   // get reference taxonomies
   $all_tax = get_object_taxonomies(\'product\');
   if ( ! empty($all_tax) ) {
     $ref_tax = wp_get_object_terms( $ref_id, $all_tax, array(\'fields\' => \'all\') );
   }
   // skip reference thumbnail and images passed as second param in function
   $skip_images = array_merge( (array)$skip_images, array($ref_thumb) );
   $args = array(
    \'post__not_in\' => $skip_images,
    \'post_type\' => \'attachment\',
    \'post_mime_type\' => \'image\',
    \'posts_per_page\' => \'-1\',
    \'post_status\' => \'inherit\',
   );
   $images = new WP_Query( $args );
   $errors = array();  
   // start loop through images  
   if ( $images->have_posts() ) :
     // after that any other function call will fail
     set_transient( \'convert_images_to_products_done\', 1);
     global $wpdb;
     while( $images->have_posts() ) :
       $images->the_post();
       global $post;
       $image = $post->ID;
       $excerpt = get_the_excerpt();
       if ( empty($excerpt) ) $excerpt = get_the_title();
       $product_vars[\'post_title\'] = get_the_title();
       $product_vars[\'post_excerpt\'] = $excerpt;
       $product = wp_insert_post( $product_vars );
       if ( intval($product) ) {
          // insert custom fields
          if ( ! empty($ref_fields) ) {
            $meta_insert_query = "INSERT INTO $wpdb->postmeta \';
            $meta_insert_query .= \'(meta_key, meta_value) VALUES ";
            $values = \'\';
            foreach ( $ref_fields as $key => $array ) {
              if ( $key != \'_thumbnail_id\' ) {
                foreach ( $array as $value ) {
                  if ( $values != \'\' ) $values .= \', \';
                  $values .= $wpdb->prepare( \'(%s, %s)\', $key, maybe_serialize($value) );
                }
              }
            }
            if ( $values != \'\' ) {
              $meta_insert_query .= $values;
              if ( ! $wpdb->query( $meta_insert_query ) ) {
                $error = \'Fail on inserting meta query for product \';
                $error .= $product . \'. Query: \' . $meta_insert_query;
                $errors[] =  $error;
              }
            }
          }
          // insert taxonomies
          if ( ! empty($ref_tax) && ! is_wp_error( $ref_tax ) ) {
            $taxonomies = array();
            foreach ( $ref_tax as $term ) {
              if ( ! isset($taxonomies[$term->taxonomy]) ) 
                  $taxonomies[$term->taxonomy] = array();
              $taxonomies[$term->taxonomy][] = $term->slug;
            }
            foreach ( $taxonomies as $tax => $terms ) {
              $set_tax = wp_set_post_terms( $product, $terms, $tax, false );
              if ( ! is_array($set_tax) ) {
                $error =  \'Fail on insert terms of taxonomy \';
                                $error .=  $tax . \' for product\' . $product;
                if ( is_string( $set_tax ) )
                                   $error .= \' First offending term \' . $set_tax;
                if ( is_wp_error($set_tax) )
                                   $error .= \' Error: \' . $set_tax->get_error_message();
                $errors[] = $error;
              }
            }
          }
          if ( ! set_post_thumbnail( $product, $image ) ) {
              $error = \'Set thumbnail failed for product \';
              $error .= $product . \' image \' . $image;
              $errors[] = $error; 
          }
       } else {
          $errors[] = \'Insert post failed for image with id \' . $image;
       }   
    endwhile;
    else : 
      wp_die(\'You have no media.\');
    endif;
     wp_reset_postdata();
    if ( ! empty($errors) )
       wp_die(\'<p>\' . implode(\'</p><p>\', $errors) . \'</p>\');
}
 。。。是的,这是一个怪物功能。。。
第三步也是最后一步是最简单的,您必须调用刚刚创建的函数,并将引用产品id作为param传递。完成此任务的方法有很多,这里只有一种:
function launch_convert_images_to_products() {
  //
  // REPLACE THE 0 IN FUNCTION PARAM BELOW WITH YOUR REFERENCE PRODUCT ID
  //
     wpa_convert_images_to_products( 0 );
  //
  //
}
add_action(\'admin_init\', \'launch_convert_images_to_products\', 30);
 请注意,通常该功能将为创建产品
all 您的图像。我将第二个参数添加到
wpa_convert_images_to_products 可以是要跳过的图像id数组的函数。
有选择地从图像创建产品的另一种方法是为附件(信息here) 然后使用适当的tax_query 在图像WP\\U查询中,选择要转换的图像。
希望有帮助。