Tags/topics: 
1
Answers
Vote up!
0
Vote down!

How to get field values from products in product display nodes?

I'd like to add some variables to some product display nodes to be used later in custom node templates. I'm currently doing this in hook_preprocess_node () **

The field values that are associated with the node displays are easy to get using something like:

<?php
$node_wrapper
= entity_metadata_wrapper('node', $vars['node']);
$field_val = $node_wrapper->field_name->value();
?>

or

<?php
field_get_items
('node', $vars['node'], 'field_name')
?>

However, I'm not sure how to go about getting values from fields in the referenced products (i.e. price, sku, etc).

Any pointers or sample code greatly appreciated!

** Not sure if this is the best approach. If anyone has a better way to do this, I'm interested!

Asked by: Dave Bruns
on June 26, 2012

1 Answer

Vote up!
1
Vote down!

Preprocess node documentation here...

http://api.drupal.org/api/drupal/modules%21node%21node.module/function/t...

Inside the template_preprocess_node() you get access to "$vars" which should give you a nice big dump of data. Link to the load_multiple function...

http://api.drupalcommerce.org/api/Drupal%20Commerce/sites%21all%21module...

And here's my function that does this...

<?php
function THEMENAME_preprocess_node($vars){
 
$product_array = array();
  foreach (
$vars['field_product'] as $product) {
   
$product_array[] = $product['product_id'];
  }
 
$products = commerce_product_load_multiple(
   
$product_array,
    array(
"status"=>1)
  );
 
// testing...
 
echo "<pre>";
 
print_r($products); exit();
}
?>
Josh Miller
Answer by: Josh Miller
Posted: Jul 6, 2012