programmatically creating a product?

Posts: 10
Joined: 12/13/2007

Hi All

I'm trying to create a product node automatically (in cron) for some uploaded photos, but I'm having a bit of trouble finding the right functions to use.
My photos have 2 attributes in a class(web and publications) - 2 different versions of the photo.

What I'm doing is using my iptc module ( http://drupal.org/project/iptc ) to extract the iptc data out of the photo (iptc.caption becomes the description, iptc.keywords go into the catalog). And I've got about 200 photos to process (and the photographer doesn't seem to want to stop taking photos!), hence doing it automatically.

So, I've got the resized images, a watermarked image suitable for the catalog, and the text for the product. Now I'm having trouble creating a full product, with catalog image and attribute image downloads.
I know I could do this in sql, but I'd prefer to do it thru drupal if possible.

Can anyone give me any pointers or point me to some uc module that does this, that might make things a bit clearer?

Cheers

Posts: 10
Joined: 12/13/2007

After reading thru' the node_save function, can I assume that if I populate the $node object with all the product related stuff, it will all get saved in the correct places?

If that's correct, then I think I just need to work out how to do the product image (is this imagecache?) and then the attribute images.

And also some way of tokenising an SKU so that it's an automatic entry.

Any help with this would really be appreciated!

Posts: 20
Joined: 12/12/2007

There are basically two ways to do this:

Doug Green outlines one based on the form api system which works pretty well but which can be a little tricky to do with attachments. http://www.civicactions.com/blog/cck_import_and_update

The second is yes, like you say, to populate a $node object so that it looks just like node_save expects it and then call node_save on it. I do this quite a bit and have found that adding a "drupal_set_message(print_r($node, true))" at the top of node_save and then submitting one example node will give me the information I need to then write the code to do it.

Good luck - let us know how it goes!

Posts: 10
Joined: 12/13/2007

Cheers for this.

Am I right in thinking the node_save should call all the relevant uc_ functions to put the data in the right places?
If I'm thinking correctly, then i would just need to find out what format the _nodeapi functions expect $node data to be in.

Also, is there anywhere I can get more info on adding attribute images as I can't seem to find anything around here that makes any sense?! (This is one of the reasons why I decided on the programmtic approach as it was taking too long to add the product, then add the attribute images, not to mention the product image)

It kind of feels like I will have to add whatever fits in the node the proper way, and then hack the other stuff directly thru' the filesystem and sql which I don't want as I want the module to be reusable at some point!

Cheers

Posts: 5617
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Yep, using node_save will invoke the appropriate hooks in any Ubercart module.

Quote:
Also, is there anywhere I can get more info on adding attribute images as I can't seem to find anything around here that makes any sense?!

I actually don't know what you mean here... attribute images is unfamiliar terminology to me. Puzzled Are you talking about using the product attribute system and associating a different image w/ an attribute? Or are you wanting to associate a separate file download based on a different product SKU? Will need a little more explanation.

Posts: 10
Joined: 12/13/2007

Hi Ryan

(great uber presentation at drupalson BTW! loved the references to "the other php ecommerce application"!!)

Yes, the product attribute system. I want to have one product that is available at 2 different image sizes.

Currently, the algorithm for what I'm doing is: (thru a cron function)

Look in a watch dir for images
(iterate thru' X amount of images, set thru' the admin function)
Find out the attribute values for each attribute of the product (settings from an admin settings page)
resample the images to the chosen size (in admin) (if they satisfy the attribute's width X height - they have to be bigger else the resulting image will be sub-standard)
extract the iptc.caption and iptc.keywords from the image to the $node (description and taxonomy/catalog)
create a watermarked version for view on the website
get the default manufacturer id
create the product
likewise create a new SKU suffixed with the attribute type - so, in this case, it is either b or s (bronze or silver)

In the admin function, I grab the attribute names, then ask the user/administrator for what the max resolutioon is for each photo attribute (if the image satisfies this, a resulting photo attribute will be produced, else not)

For the SKU, I'm just going to create a table or variable that keeps a count - it would be easier to use variable_set/get as this would impact less on database hits. In which case, I think I would need to create some kind of SKU template to use to mask the actual number.

So, from what you and greggles said earlier, I can populate $node with all the required data fields. And the varying images?
I noticed there is a function in uc_file or uc_images that will refresh the files directory, so I can get a file id (fid) for the images to add to the product.

I think I just need confirmation/affirmation that this is the way to do things....node first, images second. But the big obstacle (for me) is the filesystem. Where/how do I specify the paths to the product attribute images and the catalog display images, and how does imagecache fit into the mix given that this is really outside the realms of uberCart?

Cheers

(note to self again: never have a 'meeting' at the pub when you've got important thoughts to program!)

Posts: 2352
Joined: 08/07/2007
AdministratoreLiTe!

You can take a look inside the uc_importer module. It populates the $node object with data from XML before it calls node_save(). You'll notice that it does that before configuring the attributes of that product. You should also see how it handles images and where it finds the path to save them. Since all of the paths are stored in the database, it doesn't really matter where you put them. Imagecache doesn't do anything until you try to display an image, so there isn't anything special you need to do for it. It's the CCK image field you have to deal with.

Posts: 10
Joined: 12/13/2007

Cheers Lyle, I'll have another look at it.

Thanks for explaining what imagecache does, that would have been my next query!!

Posts: 1376
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

fronbow,

I was wondering if you'd made any progress on this? The major feature of our new website is going to be the ability for a User to create a new product. This is different from purchasing a node because, anyone who's a member can submit a new product (and we in turn share the revenue with them).

Since our products are downloadable, I'd like to restrict that to be the case for these submitted products, and integrate this with the uc_file (File Download) Feature as well as imagecache, to allow for upload of thumbnails and product images. The catalog would be similar to our current one: http://shop.rifftrax.com.

Does this sound similar to what you're trying to do? I'd love to see any code you've got currently; otherwise I'd like to help if I can. But before I start from scratch, anything you've already gotten done would prove to be an immense help. Please let me know - thanks!

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 10
Joined: 12/13/2007

I managed to get it all working on xmas eve!

Currently, the photographer uploads tagged photos to a watch dir, then when cron kicks in (a script) creates the derivative images from the original (the web version is watermarked) and sticks them in the product dir. Also, a smaller version suitable for the webpage is created from the watermarked version - I think this needed to go in the images directory so that imagecache could pick it up.
Due to the size of the original photos, I'm not allowing anyone to upload new products at the moment (I don't want to kill apache with huge uploads!!), but my next update will be to extract the author tag from the image and use this as the manufacturer so that we can start selling other people's photos.

For me, the hardest part was getting the adjustments and features to work. What I did have to hardcode was a way of generating an sku automatically, so when the product is created via cron the script gets the sku from a table and updates this table to reflect the current product. (Simply, the table consists of an autonumber field, and however many derivative fields) - you've got to have a different sku for each feature otherwise someone can purchase a 'free' photo and have access to the paid version as well.

I can share some of my code if it might help, or I can help you with sorting yours out? (But, I'd prefer not to receive requests for sharing the module as it has some pretty specific server requirements, and it's what I'm getting paid for!)
You should be able to contact me thru' drupal: http://drupal.org/user/53280/contact

cheers

Posts: 12
Joined: 12/08/2007

Could you share the code you used to add features to products? That would be very helpful.

Posts: 1
Joined: 12/01/2008

Hi

Is there any progress on this?

Looks like this was last touched about this time last year, can anyone help out?

Cheers

Miles