15 replies [Last post]
bloke_zero's picture
Offline
Joined: 03/24/2009
Juice: 62
Was this information Helpful?

I've got a thousand nodes set up as a an old content type with a cck field for price. rather than go through by hand is there a way to copy from one to another. I'm guessing that it might be some mysql+php? Or is it something that could just be done in phpMyadmin or even a module?

------------------------------
http://zero-design.info

Cayenne's picture
Offline
Joined: 01/01/2009
Juice: 533
Bloke I note that the price

Bloke

I note that the price is pretty nicely set out in the uc_products table. A mySQL query should allow you to populate that field from CCK fields (once you get a handle on where THOSE are)

I wonder whether Node Convert might work on these? If so, you'd be in fat city, since that module manually assigns data from one field to another. I suspect not, I'd at least look at it though.

bloke_zero's picture
Offline
Joined: 03/24/2009
Juice: 62
Re: Bloke I note that the price

Thanks for the suggestions Cayenne.

The first thing I tried was looping through with a node load and a node save - didn't work for me as it created a thousand errors regarding the taxonomy tables and lost a load of taxonomy stuff on the nodes (something to do with illegal offsets in the table).

In the end I did this:

$node_type = 'product';
$admin = user_load(array('name' => 'admin'));
$new_node = array('uid' => $admin->uid, 'name' => $admin->name, 'type' => $node_type);
$result = db_query("SELECT node.nid FROM {uc_products} INNER JOIN {node} ON uc_products.nid = node.nid WHERE node.status = 1");
while ($node = db_fetch_object($result)) {
$nid = $node->nid;
$product = node_load($node->nid);
$sell_price = $product->field_book_price[0]['value'];
db_query("UPDATE {uc_products} SET sell_price = '%s' WHERE nid = %d", $sell_price, $nid);
}

Which seemed to work pretty well - I had to bump up the memory to 128MB to get the script to complete, but that was it.

------------------------------
http://zero-design.info

Cayenne's picture
Offline
Joined: 01/01/2009
Juice: 533
Re: Re: Bloke I note that the price

Nicely done!

sesameseed77's picture
Offline
Joined: 09/27/2009
Juice: 23
Amazing!

Thanks, you just saved me countless hours!

I just copied the code above into my theme, changed the "field_book_price" to my cck field name "field_price", uploaded it, ran the homepage once, deleted the code from my theme and viola! All of my cck price fields are now ubercart prices!

I'm so happy that there are people out there sharing and helping each other. Mostly, I'm happy I'm not going to spend the day doing data entry!

Thanks!

ch_masson's picture
Offline
Joined: 10/14/2009
Juice: 2
Re: Amazing!

Thanks for this very valuable tip.

One more question though: where exactly did you paste this code in your theme? In which file? Where in that file?

Thanks! Smiling

sesameseed77's picture
Offline
Joined: 09/27/2009
Juice: 23
Re: Re: Amazing!

Um, it's been a while. I believe I just copied it into my main template file, which is page.tpl.php. Where really isn't important, as long as it's between the php tags.

sesameseed77's picture
Offline
Joined: 09/27/2009
Juice: 23
Re: Re: Re: Amazing!

For me, I copied it into "page.tpl.php" at /public_html/sites/all/themes/nameOfTheme

bloke_zero's picture
Offline
Joined: 03/24/2009
Juice: 62
where to paste

I actually had a page I was loading with the Drupal bootstrap stuff in it, but I would have thought the best place would be to do it would be the Devel module's paste PHP block, but I haven't actually tried it...

With D6 you can probably achieve the same thing with http://drupal.org/project/views_bulk_operations which is really useful for all sorts of tricky bulk changes.

------------------------------
http://zero-design.info

Cayenne's picture
Offline
Joined: 01/01/2009
Juice: 533
Re: where to paste

If you are just running code once to do some sort of cleanup, you can paste it into a blank page (input type PHP), and it will execute when you hit "save" or even "preview"

Cheap trick, but it works.

donalddonckers's picture
Offline
Joined: 08/13/2010
Juice: 10
Hi there, I’m using ubercart

Hi there,

I’m using ubercart marketplace because I want my users to be able to add products to the site, but I don't want them to set the sell price.
The sell price needs to be calculated based on a value in a cck field.

My idea was to use UC power tools to hide the sell price and use rules in combination with the above code to set the sell price automatically each time a node is published or updated.

Is this possible or is there a better solution?

Many thanks,
D

bloke_zero's picture
Offline
Joined: 03/24/2009
Juice: 62
use hook_nodeapi

Setting the sell price from the CCK field somehow in hook_nodeapi ( http://api.drupal.org/api/function/hook_nodeapi ) is probably your best bet - you might be able to do it using rules, but it'd be a simple module to write. If UC power tools doesn't hide it you could add that into hook_form_alter in the module so that the sell price is a hidden field.

it'd look something like this (not checked the code)

<?php
/**
* Implementation of hook_nodeapi().
*/
function module_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch (
$op) {

    case

'insert':
     
// The node is being created (inserted in the database).
   
case 'update':
     
// The node is being updated.
           
$node->sell_price = $node->myCCKfield['value']+$commision;//Calculation here
           
break;

  }
}

?>

note insert falls through to update so you perform the same operation on node create and update.

------------------------------
http://zero-design.info

donalddonckers's picture
Offline
Joined: 08/13/2010
Juice: 10
Automatically calculate sell price from a cck field

First thank you soo much for your quick reply, but I wasn't able to get it to work.
I used the following code in a module but this didn't do anything.

<?php
/**
* Implementation of hook_nodeapi().
*/
function uc_calculate_sell_price_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
   
$var1 = 0.005;
   
$var2 = 2;
   
$var3 = 0.1;
   
$var4 = 1;
  switch (
$op) {
    case
'insert':
     
// The node is being created (inserted in the database).
   
case 'update':
     
// The node is being updated.
           
$node->sell_price = ($node->field_bladzijden[0]['value'] * $var1) + ((($node->field_bladzijden[0]['value'] / $var2) + $var2) * $var1) + $var3 + $var4 + $commision; //Calculation here
           
break;
  }
}

?>

However when I do my best to read the code of the power tools module I get the impression that they use a hook_form_alter to change the sell price to a default value.
Is this a solution?

bloke_zero's picture
Offline
Joined: 03/24/2009
Juice: 62
Re: Automatically calculate sell price from a cck field

uh - sorry - I thought that would work - seems to work in some contexts. I think it should get saved automatically - did you try node_save($node)?

hook_form_alter could work - effectively intercepting the value before it gets to the DB and substituting your own value for sell_price.

http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....

you'll need your own submit function (click through to menu.module example) and find the value in $form_state and alter it?

Sorry I'm not being much help - friday afternoon brain...

------------------------------
http://zero-design.info

donalddonckers's picture
Offline
Joined: 08/13/2010
Juice: 10
Re: Automatically calculate sell price from a cck field

I got it to work! Sticking out tongue However every time I save the node I'm getting an "503 error" and I had to set the commission variable because that didn't do anything.
Maybe any suggestions how to improve this code?

<?php

/**
* Implementation of hook_nodeapi().
*/
function uc_calculate_sell_price_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
    global
$user;
   
$var1 = 0.005;
   
$var2 = 2;
   
$var3 = 0.1;
   
$var4 = 1;
   
$commission = 1;
  switch (
$op) {
    case
'insert':
     
// The node is being created (inserted in the database).
   
case 'update':
     
// The node is being updated.
           
$node->sell_price = ($node->field_bladzijden[0]['value'] * $var1) + ((($node->field_bladzijden[0]['value'] / $var2) + $var2) * $var1) + $var3 + $var4 + $commission;
           
node_save(&$node);
            break;       
  }
}

?>

Anyway thank you soo much!!!

donalddonckers's picture
Offline
Joined: 08/13/2010
Juice: 10
Got it to work properly! My

Got it to work properly!

My solution:

<?php
/**
* Implementation of hook_nodeapi().
*/
function uc_calculate_sell_price_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
    global
$user;
   
$var1 = 0.005;
   
$var2 = 2;
   
$var3 = 0.1;
   
$var4 = 1;
   
$commission = 1;
  switch (
$op) {
    case
'insert':
     
// The node is being created.
           
$node->sell_price = ($node->field_bladzijden[0]['value'] * $var1) + ((($node->field_bladzijden[0]['value'] / $var2) + $var2) * $var1) + $var3 + $var4 + $commission;
            break;
    case
'presave':
     
// The node is being updated.
           
$node->sell_price = ($node->field_bladzijden[0]['value'] * $var1) + ((($node->field_bladzijden[0]['value'] / $var2) + $var2) * $var1) + $var3 + $var4 + $commission;
            break;       
  }
}
?>

Update didn't work for me, my guess is that it had something to do with varnish that I also installed on the site.

Thanks again bloke_zero for your help! Smiling