Discussions

Displaying all images of referenced products in product display node

After hours of work i got this solution.

<?php
/**
 * Implements hook_entity_prepare_view().
 */
function HOOK_entity_prepare_view($entities, $type) {
       
 
$product_display_entity_type = 'node';
 
$product_display_type = 'dishes_product_display';
   
 
// create product display gallery with all referenced product images
 
foreach($entities as $display) {
    if(
$type == $product_display_entity_type && $display->type == $product_display_type){
             
     
$product_entity_type = 'commerce_product'
     
$product_field_name = 'field_product';
     
$photos_field_name = 'field_product_photos';
             
     
// get products ID's       
     
$product_field_language = field_language($product_display_entity_type, $display, $product_field_name);   
     
$field_product = $display->{$product_field_name}[$product_field_language];     
     
// get products image fields     
     
foreach($field_product as $product_id) {
       
$product = commerce_product_load($product_id['product_id']);   
       
$photos_field_language = field_language($product_entity_type, $product, $photos_field_name);
       
// merge produts images with product display images
       
foreach($product->{$photos_field_name}[$photos_field_language] as $image) {
         
$product_field_language = field_language($product_display_entity_type, $display, $photos_field_name);
         
$display->{$photos_field_name}[$product_field_language][] = $image;
        }      
      }                 
    }
  }   
}
?>

Improve and use it =)

Posted: May 11, 2012

Comments

nelslynn on June 20, 2012

Any clues on how to implement the code above? I'm thinking you build a custom module with this? Then what?
What is $product_field_name ?