array( '#type' => 'select', '#title' => t('Replace radio options displayed by product field'), '#description' => t('Select which product field to use for replacement'), '#options' => _commerce_product_field_product_attribute_taxonomy_field_options($bundle), '#default_value' => (isset($commerce_cart_settings['replace_product_field']) ? $commerce_cart_settings['replace_product_field'] : 'commerce_default'), '#required' => false, '#states' => array( 'visible' => array( ':input[name="instance[commerce_cart_settings][attribute_widget]"]' => array('value' => 'radios'), ), ), ), ); } } /** * Create an option list of available product fields to replace the taxonomy terms within the selected vocabulary * @param $bundle * The product fields * @return array */ function _commerce_product_field_product_attribute_taxonomy_field_options($bundle) { $fields_list = field_info_instances('commerce_product', $bundle); $options = array(); $options['commerce_default'] = 'Use Commerce default behavior'; foreach($fields_list as $option) $options[$option['field_name']] = $option['label']; return $options; } /** * Implements hook_form_FORM_ID_alter(). */ function commerce_product_field_product_attribute_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state, $form_id) { //If there isn't enabled product to be displayed, we leave the function now if(!isset($form_state['default_product'])) { return; } //First, get all cart enabled fields of the current product entity being displayed. //Get the current product type. $current_product = $form_state['default_product']; //Get all the current product type extra fields. $fields_info = field_info_instances('commerce_product', $current_product->type); //Create an array of attribute options to replace with taxonomy values $options = array(); foreach ($fields_info as $field_info) { if (isset($field_info['commerce_cart_settings']) && isset($field_info['commerce_cart_settings']['attribute_widget']) && isset($field_info['commerce_cart_settings']['attribute_field']) && isset($field_info['commerce_cart_settings']['replace_product_field'])) { //If the replace_product_field attribute is "commerce_default" we don't do anything for this field if($field_info['commerce_cart_settings']['attribute_widget'] == 'radios' && $field_info['commerce_cart_settings']['replace_product_field'] != 'commerce_default') { $options[] = array( "product_field" => $field_info['field_name'], "replacement_field" => $field_info['commerce_cart_settings']['replace_product_field'], ); } } } //Alter the display of taxonomy term reference fields. foreach($options as $current_altered_field) { //Check if the field is to be displayed in the current display if(!isset($form['attributes'][$current_altered_field['product_field']]['#options'])) continue; //Get all of the product ids for the current form $line_item = $form_state['line_item']; $entity_data = $line_item->data['context']['entity']; $entity = entity_load_single($entity_data['entity_type'], $entity_data['entity_id']); $product_ids = entity_metadata_wrapper($entity_data['entity_type'], $entity)->{$entity_data['product_reference_field_name']}->raw(); // Load all the active products intended for sale on this form. $products = commerce_product_load_multiple($product_ids, array('status' => 1)); // The radio buttons are displayed based on the term id's, so we have to couple the selected product field to the term id's. foreach ($products as $product) { // determine product term id $replace_term_field = field_get_items('commerce_product', $product, $current_altered_field['product_field']); $tid = !empty($replace_term_field[0]['tid']) ? $replace_term_field[0]['tid'] : $product->product_id; // get the renderable version of the replacement field $product_renderable = commerce_product_build_content($product); if(!empty($product_renderable['commerce_product'][$product->product_id][$current_altered_field['replacement_field']][0])) { $form['attributes'][$current_altered_field['product_field']]['#options'][$tid] = render($product_renderable['commerce_product'][$product->product_id][$current_altered_field['replacement_field']][0]); } } } }