php Woocommerce产品创建自动化

rekjcdws  于 2022-11-28  发布在  PHP
关注(0)|答案(1)|浏览(161)

我正在尝试开发一个程序,根据上传到wordpress库的媒体创建woocommerce产品。
所有产品都有相同的设置,唯一改变的是照片和下载。
通过一些研究,我成功地开发了以下代码,它在functions.php中运行:

add_action( 'init', 'product_automation');

function product_automation() {
  $loopProdutos = new WP_Query([
    'post_type' => 'product',
    'posts_per_page' => -1
  ]);
  $arrayProduct = (array) $loopProdutos->posts;
  // Picking up all the products from the store

  $idImgProduct = [];
  foreach($arrayProduct as $product) {
    $produto = wc_get_product($product->ID);
    $idImgProduct[] = $produto->image_id;
  }
  // Taking all photo IDs from existing products

  $loopImagens = new WP_Query([
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'orderby' => 'post_date',
    'order' => 'desc',
    'posts_per_page' => -1,
    'post_status' => 'inherit'
  ]);
  $arrayImg = (array) $loopImagens->posts;
  // Taking all uploaded photos

  $idImg = [];
  foreach($arrayImg as $image) {
    $idImg[] = $image->ID;
  }
  // Getting all uploaded photo IDs

  $newIds = array_diff($idImg, $idImgProduct);
  // Seeing which IDs do not yet exist

  $newProductsArray = [];
  // Array with new products

  foreach($newIds as $idImagem) {
    $objProduct = new WC_Product_Simple();
    $objProduct->set_name("Foto");
    $objProduct->set_status("publish"); 
    $objProduct->set_catalog_visibility('visible');
    $objProduct->set_price(15.00);
    $objProduct->set_regular_price(15.00);
    $objProduct->set_sold_individually(true);
    $objProduct->set_image_id($idImagem);
    $objProduct->set_downloadable(true);
    $objProduct->set_virtual(true);
    // Create a simple product
    
    $src_img  = wp_get_attachment_image_src( $idImagem, 'full');
    $img_meta = wp_get_attachment_metadata( $idImagem, false );
    
    $file_title = $img_meta['image_meta']['title'];
    $file_url   = reset($src_img);
    $file_md5   = md5($file_url);
    
    $download  = new WC_Product_Download();
    
    $download->set_name($file_title);
    $download->set_id($file_md5);
    $download->set_file($file_url);
    
    $downloads[$md5_num] = $download;
    
    $objProduct->set_downloads($downloads);
    // Code to automatically add download link to product

    $newProductsArray[] = $objProduct;
  }

  for ($i = 0; $i < count($newProductsArray); $i++) {
    $newProductsArray[$i]->save();
  }
  //Loop to save each new product
}

代码工作得很好。问题是它创建的产品比预期的要多。
例如,如果我的图库中有10张照片,它将创建16或20个产品(每张照片生成多个产品)。
我找不到错误。需要帮助。😅

mnowg1ta

mnowg1ta1#

在这里,你去,我已经测试了它,它似乎工作,除了“附加图像到职位”(我的意思是,它显示为产品特色的图像,但在媒体下,它说,它没有附加到任何东西,你不能“分离”)。
我找到了另一个钩子,它应该比wp_handle_upload更好用,并且还修复了几个bug(图片标题和下载数组):

add_action( 'add_attachment', 'bbloomer_product_automation', 9999 );

function bbloomer_product_automation( $image_id ) { 
    $product = new WC_Product_Simple();
    $product->set_name( 'Foto' );
    $product->set_status( 'publish' ); 
    $product->set_catalog_visibility( 'visible' );
    $product->set_price( 15 );
    $product->set_regular_price( 15 );
    $product->set_sold_individually( true );
    $product->set_image_id( $image_id );
    $product->set_downloadable( true );
    $product->set_virtual( true );      
    $src_img = wp_get_attachment_image_src( $image_id, 'full' );
    $file_title = get_the_title( $image_id ); // CORRECT GET IMAGE TITLE
    $file_url = reset( $src_img );
    $file_md5 = md5( $file_url );
    $download = new WC_Product_Download();
    $download->set_name( $file_title );
    $download->set_id( $file_md5 );
    $download->set_file( $file_url );
    $downloads[$file_md5] = $download; // BUG FIXED
    $product->set_downloads( $downloads );
    $product->save();
}

相关问题