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)?

  • 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 Mar 31 '14 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 Apr 1 '14 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 Apr 1 '14 at 7:25
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 );
  }
});
  • 1
    Just to clarify this is called when an attachment is created, which means that existing images in your media library will not be parsed. – codearachnid Apr 3 '14 at 14:30
  • 1
    @codearachnid yep, sure. I did not specify that in the answer because OP explicitly says: "I am planning to upload images in future"... – gmazzap Apr 3 '14 at 20:09
1

Based on your comment to your question, you might take a look at http://www.wpallimport.com 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.