Getting CCK field value from $products?

Posts: 51
Joined: 12/16/2007

I need some help with a very basic problem. I am working on a freight module for UC and I need to get a handle on a CCK field value from the module.

I wrongly assumed that a product's CCK value would get passed to the module function with the $products item.

<?php
function uc_freight_rate_request($origin, $destination, $products) {

foreach (
$products as $product) {
   
$items[] = array(
           
'Class' => $product->field_freight_class, // CCK field
   
);

}
?>

The CCK field name is field_freight_class, how do I get a handle on it from my function?

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

The Devel module is very helpful. With it enabled, you can use

<?php
  dpm
($product);
?>

This is equivalent to
<?php
  drupal_set_message
('<pre>'. print_r($product, true) .'</pre>');
?>

which will show you the entire structure of the $product variable.

Posts: 51
Joined: 12/16/2007

Thanks for the help Lyle. When I dump $product I get this for the CCK field:

<?php
[field_freight_class] => Array
        (
            [
0] => Array
                (
                    [
value] => 50
               
)
        )
?>

The problem is I'm not sure what to do with this information.

So for this routine in the function how would I get a handle on the CCK value? I've tried everything I could think of without success. This just seems to tricky to me.

<?php
foreach ($products as $product) {
    
$items[] = array(
   
'Class' => $product->'* How do I reference the CCK value? *',
   
'Weight' => $product->weight * $product->qty,
     )
?>

Thanks again for your help!

Complete Computer Care | The Cosmic Gift | Florida Fossils | Team Hope Evangelism

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

dpm() is showing you that it's several nested arrays, so you have to access them that way.

<?php
  $product
->field_freight_class[0]['value']; // has a value of 50
?>

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

EDITED since Lyle beat me to it.

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com