Create a gist now

Instantly share code, notes, and snippets.

Embed
<?php
/**
* Implements hook_form_commerce_cart_add_to_cart_form_alter(&$form, &$form_state).
*/
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);
$fieldname = 'commerce_product';
foreach ($prod_objects as $key => $value) {
$items = reset(field_get_items($fieldname, $value, 'field_type_image'));
$title = field_get_items($fieldname, $value, 'title');
$uri = $items['uri'];
$title = $value->title;
$c = reset(field_get_items($fieldname, $value, 'commerce_price'));
$currency = commerce_add_to_cart_form_get_currency($c);
$variables['path'] = file_create_url(image_style_path('thumbnail', $uri));
$variables['attributes'] = '';
$form['product_id']['#options'][$key] = $title . theme_image($variables) . $currency;
}
$form['product_id']['#type'] = 'radios';
}
/**
* Gets currency but accounts for commerce_multicurrency.
*/
function commerce_add_to_cart_form_get_currency($c) {
if (module_exists('commerce_multicurrency')) {
$target_currency_code = commerce_multicurrency_get_user_currency_code();
$cur = commerce_multicurrency_conversion($c['amount'], $c['currency_code'], $target_currency_code);
$currency = commerce_currency_format($cur, $target_currency_code);
}
else {
$currency = commerce_currency_format($c['amount'], $c['currency_code']);
}
return $currency;
}
@lsolesen

This comment has been minimized.

Show comment
Hide comment
@lsolesen

lsolesen Oct 13, 2012

I cannot get the code from the article to work properly. I rewrote it for less duplication, but still not working.

Owner

lsolesen commented Oct 13, 2012

I cannot get the code from the article to work properly. I rewrote it for less duplication, but still not working.

@jodyHamilton

This comment has been minimized.

Show comment
Hide comment
@jodyHamilton

jodyHamilton Sep 10, 2013

The above code didn't work. My version:

/**

  • Implements hook_form_commerce_cart_add_to_cart_form_alter().
    */
    function CUSTOM_form_commerce_cart_add_to_cart_form_alter(&$form, $form_state) {
    // Add prices to attribute selectors.
    if (isset($form['attributes'])) {
    $entity = reset(entity_load($form_state['context']['entity_type'], array($form_state['context']['entity_id'])));
    foreach (element_children($form['attributes']) as $field) {
    foreach (field_get_items($form_state['context']['entity_type'], $entity, 'field_product') as $product) {
    $product = commerce_product_load($product['product_id']);
    $attribute = reset(field_get_items('commerce_product', $product, $field));
    $attribute = $attribute['value'];
    $item = reset(field_get_items('commerce_product', $product, 'commerce_price'));
    $price = commerce_currency_format($item['amount'], $item['currency_code'], $product);
    $form['attributes'][$field]['#options'][$attribute] = $attribute . ' ' . $price;
    }
    }
    }
    }

The above code didn't work. My version:

/**

  • Implements hook_form_commerce_cart_add_to_cart_form_alter().
    */
    function CUSTOM_form_commerce_cart_add_to_cart_form_alter(&$form, $form_state) {
    // Add prices to attribute selectors.
    if (isset($form['attributes'])) {
    $entity = reset(entity_load($form_state['context']['entity_type'], array($form_state['context']['entity_id'])));
    foreach (element_children($form['attributes']) as $field) {
    foreach (field_get_items($form_state['context']['entity_type'], $entity, 'field_product') as $product) {
    $product = commerce_product_load($product['product_id']);
    $attribute = reset(field_get_items('commerce_product', $product, $field));
    $attribute = $attribute['value'];
    $item = reset(field_get_items('commerce_product', $product, 'commerce_price'));
    $price = commerce_currency_format($item['amount'], $item['currency_code'], $product);
    $form['attributes'][$field]['#options'][$attribute] = $attribute . ' ' . $price;
    }
    }
    }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment