I am working on converting commerce theme to Drupal Template and have difficulty understand how the Product attribute selection works in the product display.

i have one product display type for multiple products. There is wide variation of products(i.e. runner shoes, tennis shoes,etc). For each product display instance i have selected few of those variations(i.e for Runner shoes i have red runner shoes small , blue runner shoes small, blue runner shoes large, etc). It works nice where user are able to select the attributes and the particular product is displayed.

In the template.php, i load the product display instance:

$node = $vars['node'];

This instance has reference to the products($node->field_product). So far so good:) The difficulty is to determine which product of these referenced is currently displayed. How can i do that? In what variable is the object or its id is stored? I see in the default html things like:


How is this all thing working? Any help or source of reference much appreciated

1 Answer 1


I'm not sure the currently displayed product would be found at the $node level in the template. Typically, when a page is first loaded, it's going to be the product referenced in the field at delta 0. This is alterable, though, so you could invoke the API function, commerce_product_reference_default_product(), in template.php and enrich the $node with that information.

After the page has been initially loaded and displayed, the "currently displayed product" may change based on user input. We store this as the "default product" in the form state variabel $form_state['default_product'] of the Add to Cart form. I assume you'd be able to propagate this from there through to your template somehow, likely through a combination of form alter and preprocess in your template file.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.