Populate shop from a folder of images

Posts: 48
Joined: 11/29/2007
Bug Finder

Hello again.

I was just wondering if there is a simple way to populate a webshop from a folder of images instead of creating each product separately.

The products in this shop will all be images, which come in 3 sizes with 3 different frames.

All images can have the same default price for each option, so that should be easy enough to setup in ubercart. I really like that options can have default prices by the way.

There's currently about 250 images and since they are all the same i want to find a nice way to build the shop.

The only way i know that might work is to make 1 product, export the table from the database, then manipulate it to include all the file names, then import it back into the database.

But ultimately i'd like the client to be able to mass upload the files.

Originally I made this site in gallery2, so i've been spoiled with mass uploading methods, watermarking, etc. But the ecommerce side of gallery2 is very lacking so i'm developing the shop in drupal+ubercart instead.

Thanks for your help,

matt

Posts: 46
Joined: 11/22/2007

I think the best way for you would be to create a small php script which does about the following:

get all the files in the appropriate image dir
check for each if the file exists in the products database
if it doesn't, add it

You could do it using direct querys or "the clean way", using drupal's node_load/node_save. If you decide which way to go, I could post some snippets if you like... The latter is prefered to keep it working with updated versions of drupal/uc.

Posts: 48
Joined: 11/29/2007
Bug Finder

That sounds like a good way to do it.

I'm pretty new to drupal and haven't really had a good look around the code.

And my php is fairly limited but i should be able to figure it out with a couple pointers. I would prefer to do it the clean way. I'm quite keen to learn how to make modules for drupal

I'd be surprized if there's no way of doing this tho. I saw a few modules that seem to automatically build nodes, i may take a closer look at those first.

Thanks for your reply.

I think a mass importer would be a great feature, especially in conjunction with the product class system.

cheers,

matt

Posts: 46
Joined: 11/22/2007

As far as importing goes, there are a few modules and options:
- uc has an xml import/export available
- uc has people working on a csv to xml conversion
- drupal has node_import (http://drupal.org/project/node_import) which can be made compatible with uc products with a small add-on here

Each of those requires some manual interaction, or you have to dive into php&cron as well to trigger them automatically.

Here's a fast run through of your basic needs:

$myfiles = ""; // the file directory you want to loop/add
if ($dir = @opendir($myfilesdir)) {
while (($file = readdir($dir)) !== false) {

  // first, let's get you a node object
  $node = new StdClass();

  // the following looks for a node with the (product) type 'imageproduct' and the filename as title
  $searchedNode = node_load(array('type' => 'imageproduct', 'title' => $file));
  if($searchedNode && $searchedNode != null && isset($searchedNode->nid) && $searchedNode->nid > 0) {
    // update mode, we set the current node to the node we're gonna save to keep all it's settings
    $node = $searchedNode;
    // extract the taxonomy ids that are already set
    $tax = array();
    foreach($node->taxonomy as $term) {
       if(is_object($term)) {
         $tax[] = $term->tid;
       }     
    }
    $node->taxonomy = $tax;
  }
  //now you can update/add node items
  $node->title = $file;
  $node->format = 3; // 1 = Filtered HTML, 2 = PHP Code, 3 = Full HTML
  $node->uid = 1; // set admin user 1 as author
  $node->body = "The body text of this node";

  //code for uc product image handling goes here - no example available, I use a cck imagefield
 
  //if the node got loaded, the id is set, else, a new id will be created for your node
  node_save($node);

closedir($dir);
}

http://api.drupal.org/api/function/node_load/5
http://api.drupal.org/api/function/node_save/5

The node handling code comes from some xmlrpc import we built for xml files containing all kinds of data, not specific ubercart, but it should get you going Eye-wink

Posts: 48
Joined: 11/29/2007
Bug Finder

Has anyone eveerr used the drupal webdav module?

It allows you to create nodes by making folders and files. very cool.

m