0

Drupal Commerce add-to-cart attribute dropdown list shows product titles, instead of label of the attribute.

One thing that could be causing this is two product types with the same attribute field on the product display. This is required as one product is downloadable and requires a different product type than the second product type used for shippable items.

Is there a way to change the dropdown to contain attribute labels?

Current: https://www.dropbox.com/s/ib03wn7qd7hpwqm/add-to-cart-dropdown.png?dl=0 Desired: https://www.dropbox.com/s/5js29glqqbxuqiz/desired-dropdown.png?dl=0

Drupal Commerce 7.x-1.13

0

Here's how this was accomplished in a custom module. This will use the taxonomy terms vs the product titles.

function custom_form_views_form_commerce_cart_form_default_alter(&$form, &$form_state) {

$vocabulary = taxonomy_vocabulary_machine_name_load('product_type'); $terms = taxonomy_get_tree($vocabulary->vid); $terms_name = array(); foreach ($terms as $term) { $terms_name[$term->tid] = $term->name; }

foreach ($form['edit_attributes'] as $key => $line_item) { if (isset($line_item['product_id']['#options']) && $line_item['product_id']['#options']) { $options = array_keys($line_item['product_id']['#options']); $products = commerce_product_load_multiple($options); foreach ($line_item['product_id']['#options'] as $id => $name) { $form['edit_attributes'][$key]['product_id']['#options'][$id] = $terms_name[$products[$id]->field_product_type['und'][0]['tid']]; } $form['edit_attributes'][$key]['product_id']['#title'] = 'Format'; } } }

function custom_form_alter(&$form, &$form_state, $form_id) {

if (strpos($form_id, 'commerce_cart_add_to_cart_form') === 0) { $vocabulary = taxonomy_vocabulary_machine_name_load('product_type'); $terms = taxonomy_get_tree($vocabulary->vid); $terms_name = array(); foreach ($terms as $term) { $terms_name[$term->tid] = $term->name; }

if (isset($form['product_id']['#options'])) {
  $options = array_keys($form['product_id']['#options']);
  $products = commerce_product_load_multiple($options);
  foreach ($options as $key) {
    $form['product_id']['#options'][$key] = $terms_name[$products[$key]->field_product_type['und'][0]['tid']];
  }
  $form['product_id']['#title'] = 'Format'; // added by Lynn. Works on product page, but no in cart
}

} }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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