Drupal Commerce add to cart form alter enabling radio, titles, product image, and price

Any e-shop owner whant to show all possible product variations (i mean variations wich depends on attributes: color, size, etc). In drupal commerce product display are shown with only one commerce product and it changes after customer select another item in select list. Being honest its not quite comfortable. That is why we have done form alter which changes select list on radio, also shows product titles, image, and price. You should just create your own custom module and put the code  given below. Moreover product price in items are convertable if commerce multicurrency module is enabled.

function hook_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state) { 
    $options = $form['product_id']['#options'];
    $op = array();
    foreach ($options as $key => $value) { 
        $op[] = $key;
    } 
    $prod_objects = commerce_product_load_multiple($op, $conditions = array(), $reset = FALSE);
    if ( module_exists('commerce_multicurrency') ) {
        $target_currency_code = commerce_multicurrency_get_user_currency_code();
        foreach ($prod_objects as $key => $value) {
        $items = reset( field_get_items( 'commerce_product', $value, 'field_type_image' ) );
        $title = field_get_items( 'commerce_product', $value, 'title' );
        $uri = $items['uri'];
        $title = $value->title;
        $c = reset( field_get_items( 'commerce_product', $value, 'commerce_price' ) );
        $cur = commerce_multicurrency_conversion($c['amount'], $c['currency_code'], $target_currency_code);
        $currency = commerce_currency_format($cur, $target_currency_code);
        $variables['path'] = file_create_url( image_style_path('thumbnail', $uri));
        $variables['attributes'] = '';
        $form['product_id']['#options'][$key] = $title . theme_image($variables) . $currency ;
        } 
    } else { 
        foreach ($prod_objects as $key => $value) {
            $items = reset( field_get_items( 'commerce_product', $value, 'field_type_image' ) );
            $title = field_get_items( 'commerce_product', $value, 'title' );
            $uri = $items['uri'];
            $title = $value->title;
            $c = reset( field_get_items( 'commerce_product', $value, 'commerce_price' ) );
            $currency = commerce_currency_format($c['amount'], $c['currency_code']);
            $variables['path'] = file_create_url( image_style_path('thumbnail', $uri));
            $variables['attributes'] = '';
            $form['product_id']['#options'][$key] = $title . theme_image($variables) . $currency ;
            } 
    } 
    $form['submit']['#value'] = t('Add to Cart'); $form['product_id']['#type'] = 'radios'; 
}
Rating: 
10 out of 10 based on 4 ratings.
Tags: