Vote up!
Vote down!

Automating product creation/updating


As I write this I'm trying to chew my way through the API docs, but I'm not having much luck stringing the parts together that I need, so I hope somebody can help a bit.

My intention is to sync products in my DC catalogue from an external data source (so all products are created and modified programmatically based on that data). The code to update the catalogue would run via cron. There will just be one SKU per product display.

Having drilled into it a little, I see I can use commerce_product_new() to create a new instance of a given product type, but a completed product (created through the UI) has many other fields such as price, images, and other fields that have been added to the variation type. Also of course this is just a SKU/Product Variation (just about got my head round the nomenclature now, I think), so a Product Display
has to be created at the same time, which has its own fields (title, body).

Can this realistically be done programmatically, or am I better off just attacking the database directly? (I'd certainly rather just use "blessed" api methods if they're available, but I'll go the hackish route if there's no other option.)

Any advice/tips would be welcome, as this is altogether to wordy to usefully google for!

Asked by: headbank
on February 24, 2014

2 Answers

Vote up!
Vote down!

Hi headbank,

It might be worth having a look at https://drupal.org/project/commerce_feeds . It will probably get you most of the way to what you're looking for, if not all the way, without much code. If it doesn't then at least you've got some functioning code examples in front of you.


Andy @ BlueFusion
Posted: Feb 24, 2014
Vote up!
Vote down!

Hi DNZ, thanks for this - it looks a lot like what I need, I've installed it and tried the example following Randy Fay's screencast, and it certainly seems to work as advertised.

I probably do need to change it a bit, at least insofar as I need the processors to do a couple of things that the built-in ones don't, e.g. applying different VAT rates (the Product Processor only allows the same rate to be applied for all items, but my catalogue comprises some VAT-liable items and some that aren't).

Also it seems to be picky about what fields it allows as mapping targets: A List(text) field that I added to the Product variation type is available, but the Weight field (from commerce_physical) doesn't show up in the dropdown.

I'm also not quite clear on how to specify (for cron purposes) where to read the file from, so that looks like another aspect I'd need to hack at.

Still, Feeds is definitely a sizeable part of the puzzle solved.

Answer by: headbank
Posted: Feb 26, 2014


In the end I decided against going with Feeds, because it just adds too much bulk to the project and duplicates work I already did. Instead I'm using the entity API to create and modify products and display nodes.

The one part I can't yet manage that way is field_images. I think I need to look into the file API to work out how to create the files I upload as "managed" file objects. If anyone has any pointers for that I'd certainly welcome them.

- headbank on March 24, 2014