Commerce - Alter quanitity widget I'm new to commerce and I have a question. I'm building a website for a butcher, and he wants products to be sold per 100gr. I have the price in kilo and I allow decimal values for quantity.

The quantity field widget is a text box by default. I did some googling and found some things but i'm unable to get it work. I assume I need the commerce_cart_add_to_cart_form() function :)

I want to have a drop down with pre-defined values per 100 gram for example.



100 gram - 1 euro
200 gram - 2 euro
300 gram - 3 euro
....

1000 gram - 10 euro

Add to cart button

Any idea's? Or is there an easyer solution for this?

Thanks in advance
Assuming that you allow decimal in the quantity field, you can use the hook_form_alter() hook. It is one of the most used drupal hooks.

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&amp;$form, &amp;$form_state, $form_id) {

  // add to cart form
  if (strpos($form_id, 'commerce_cart_add_to_cart_form_') === 0) {

    // Replace the text quantity field with a select list.
    if (isset($form['quantity']) &amp;&amp; ($form['quantity']['#type'] == 'textfield')) {

      $product = commerce_product_load($form['product_id']['#value']);
      $price = commerce_product_calculate_sell_price($product);

      $select = array(
        '0.1' =&gt; '100grams '.commerce_currency_format(1*$price['amount'], $price['currency_code'], $product),
        '0.2' =&gt; '200grams '.commerce_currency_format(2*$price['amount'], $price['currency_code'], $product),
        '0.3' =&gt; '300grams '.commerce_currency_format(3*$price['amount'], $price['currency_code'], $product),
        '0.4' =&gt; '400grams '.commerce_currency_format(4*$price['amount'], $price['currency_code'], $product),
      );

      $form['quantity']['#type'] = 'select';
      $form['quantity']['#options'] = $select;
      $form['quantity']['#size'] = 1;
    }
  }
}