最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - How to add product images using script?

programmeradmin3浏览0评论

I have few hundred products on woocommerce without images. I am planning to upload images in future. Images have same name as the SKU of product (+.jpg).

Is there a way I could write a script that adds this image as product image and featured image of the SKU = name of this image(-.jpg)?

I have few hundred products on woocommerce without images. I am planning to upload images in future. Images have same name as the SKU of product (+.jpg).

Is there a way I could write a script that adds this image as product image and featured image of the SKU = name of this image(-.jpg)?

Share Improve this question asked Mar 28, 2014 at 18:19 Tasawer KhanTasawer Khan 711 gold badge1 silver badge8 bronze badges 3
  • 5 This is certainly possible and btw not especially woocommerce specific. Anyhow, you definitely should improve your question, because this way it is clearly a do my work for me request, which isn't how WordPress Development works. The intention on WordPress Development is rather to help you with a problem you already researched and approached. That said, complete this with some groundwork, research and code, and the chances of getting the help you want will get significantly higher. – Nicolai Grossherr Commented Mar 31, 2014 at 14:13
  • +1 for @ialocin and, just for example, what have you tried? What have you find googling for your problem? And regarding the details of your request, do you plan to upload files via FTP or via WordPress media manager? – gmazzap Commented Apr 1, 2014 at 5:46
  • Thank You ialocin and G. M. I am planning to upload files using media Manager . I thought my question as "have anyone done this, if yes, how?" . I was expecting someone to suggest a plugin that might be able to do this(csv import plugin) or a snippet that updates products. – Tasawer Khan Commented Apr 1, 2014 at 7:25
Add a comment  | 

2 Answers 2

Reset to default 5 +50

You don't need a plugin, if your images names really match with product SKUs and you use media uploader to upload the file, you can use a single function hooked in add_attachment and you are done.

Once it require some work, I'd avoid to upload all the hundreds of images in one "session", probably you can upload sets of 25/50 images per time with no issues...

See comments for further informations on what code does:

add_action('add_attachment', function( $attachmentID ) {
  if ( ! class_exists( 'WC_Product' ) ) return; // if no WooCommerce do nothing

  // an attachment was jus saved, first of all get the file name
  $src = wp_get_attachment_image_src( $attachmentID, 'full' );
  $filename = pathinfo( $src[0], PATHINFO_FILENAME );

  // now let's see if exits a product with the sku that match filename
  $args = array(
    'meta_key' => '_sku',
    'meta_value' => $filename,
    'post_type' => 'product',
    'posts_per_page' => '1' // assuming sku is unique get only one post
  );
  $prods = get_posts( $args );
  if ( ! empty($prods) ) {

    // ok we have a match, exists a product having sku that match filename
    $product = array_pop( $prods );

    // set the thumbnail for the product
    set_post_thumbnail( $product, $attachmentID );

    // now "attach" the post to the product setting 'post_parent'
    $attachment = get_post( $attachmentID );
    $attachment->post_parent = $product->ID;
    wp_update_post( $attachment );
  }
});

Based on your comment to your question, you might take a look at http://www.wpallimport they have a woocommerce addon that works pretty well and includes the product image importer. With a custom CSV i'm sure you could wrangle it to import all in one import for you (including the media manager bit).

Now if you are looking to attach existing uploaded media items via a script then it should be as simple as querying your unattached images inspecting their image name (unless you are customizing them) and then doing a meta query sku lookup on post_type = 'product' and then run update_post_meta($post_id, '_thumbnail_id', $attachment_id); with the supplied bits of info. Depending on the amount of images you're looking to parse through + hosting environment this could take a long time or run out of resources/time during execution. So I'd suggest adding a pagination component to throttle your execution process/time down into managable chunks for the environment.

发布评论

评论列表(0)

  1. 暂无评论