4 replies [Last post]
tilmankoester's picture
Offline
Joined: 03/20/2010
Juice: 13

Hi,
So i've added a computed field to my product node-type which gives me numbers sold of this product. I had to add it to the db, because i want to use it in views.
The problem now: How can I update that field? As most of you might know, these fields only update, when you go into that node/*/edit and press 'save' again.
Obviously that's not ideal solution, because I don't want to click 'save' everytime I sell something Eye-wink
I have two possible solutions in mind:

1) I thought about Rules module. "Trigger: Product gets sold Action: Update product node" (which is equivalent to saving the node again)
Problem is; I just don't know how to integrate Rules module with Ubercart.

2) Update it once a day via cron run. Cons: Not 'live' and if there are a lot of nodes it will take a while. Also hook_cron() could be documented better Eye-wink

Can anyone guide me the right direction? This has been doing my head in for the last two days already. Help much appreciated.

tilmankoester's picture
Offline
Joined: 03/20/2010
Juice: 13
Re: Update CCK Computed Field

Sorry for the double post. Just thought i'd share my current setup, updating it with every cron run.

/**
* Implementation of hook_cron().
*/
function your_module_cron() {
  $product_types = module_invoke_all('product_types');
  $products = array();
  $result = db_query('SELECT nd.nid FROM {node} AS nd WHERE nd.type IN ("'.implode('","', $product_types).'")');
  while ($row = db_fetch_object($result)) {
    $products[] = $row->nid;
  }
  foreach ($products as $product) {
    node_save(node_load($product, NULL, TRUE));
  }
}
vintage's picture
Offline
Joined: 04/01/2010
Juice: 3
an idea from a non ubercart'er

Have you tried the Dynamic Field CCK module? I'm not sure if it will work but that's what I use on my site. You can basically put any PHP code into a CCK field and it'll run without the user even knowing it's running. I have no idea if that sort of stuff works in ubercart because I've honestly never used ubercart before (I found your post thru google when I was trying to do what you're doing.

http://drupal.org/project/dynamicfield

Good luck!
Eric

Uberchic's picture
Offline
Administrator
Joined: 08/29/2008
Juice: 754
Re: Update CCK Computed Field

conditional actions is ubercart's answer to rules. You can read more about conditional actions here:
http://www.ubercart.org/docs/developer/9034/conditional_actions
and here:
http://www.ubercart.org/docs/user/7657/configuring_conditional_actions

tilmankoester's picture
Offline
Joined: 03/20/2010
Juice: 13
hi thanks

yes, i ended up writing a new action and predicament, so everytime an order gets completed i recalculate the sold field of the products in that order.
i think that is more economical than cron Eye-wink