Skip to main content
5 of 5
clarification

computed field with product variation and content type

Using Commerce Kickstart 2 distribution :

Question 1: Where should the computed field go to access fields from both the Content Type and Product Variation? Does the computed field belong to the content type or to the product variation?

Question 2: How do you reference one from the other?

ie - I thought...

$field_my_gauge = field_get_items($entity_type, $entity, 'field_needle_guage');
$field_my_type = field_get_items($entity_type, $entity, 'field_needle_type');
$field_my_st = field_get_items($entity_type, $entity, 'field_needle_style');
$field_my_len = field_get_items($entity_type, $entity, 'field_needle_length');
$vari_l = taxonomy_term_load($field_my_len[0]['tid']);
$vari_g = taxonomy_term_load($field_my_gauge[0]['tid']);
$vari_t = taxonomy_term_load($field_my_type[0]['tid']);
$vari_s = taxonomy_term_load($field_my_st[0]['tid']);
$vari_result =  $vari_l->name . $vari_g->name . $vari_t->name . $vari_s->name;
$vari_result = preg_replace('/[\s,.]{1,}/', '', $vari_result);
$entity_field[0]['value'] = $vari_result;

buuuut...

Notice: Trying to get property of non-object in eval() (line 9 of /Users/Robert/Sites/devdesktop/country_yarns/modules/computed_field/computed_field.module(468) : eval()'d code).

$vari_g is, or should, be based on the field_needle_guage field. But this is in the product variation of Needle, and not in the content type of Needle. All other fields are in the content type and work correctly.

So what's the right way to access members of a product variation in the content type - or is that even the right way around? (See Q1)

[EDIT]

Ok, so looking at the objects available in the wrapper:

$wrapper = entity_metadata_wrapper($entity_type, $entity);
// field_product_node is documented as the automatically created backreference to the content type.
$product = $wrapper->field_product_node;

// DrupalCommerce Kickstart 2 documentation says the below should work, but it does not - 'error: can't find getProduct function'. (WHY?)
$wrapper->getProduct();

// This comes out as an EntityDrupalWrapper
dpm($wrapper);
// This comes out as an EntityListWrapper
dpm($product);

// but below fails - can't find getIterator on object (WHY??)
$plist = $product->getIterator();
// and below returns an empty array! 
$plitems = $product->value(); 

So all these auto-back-reference class things in DrupalContrib are not accessible in this manner, it seems. If they work, I'd love to find out how!

Looking at how the tables are set up: field_revision_field_product is the link table between 'commerce_product' 'commerce_product_revision' and 'node', and any particular field_data_field_XXX field data table.

So I guess I need to use a sql query to get the field_data_field_XXX value through an inner join. I guess I'll need to put that into a custom module and then make a field of that type that allows you to configure what field to grab from the CT