I have two kinds of price type: price per unit and price per weight. I have managed to use hook_form_alter()
to change the widget textbox to a predefined select list.
For some items it fills the selectbox with '1 piece + price', '2 pieces + price'; for other items, I use '100 grams + price', '200grams + price' and so on. The code I have used for this is below.
function kmsol_quantity_form_alter(&$form, &$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']) && ($form['quantity']['#type'] == 'textfield')) {
$product = commerce_product_load($form['product_id']['#value']);
$price = commerce_product_calculate_sell_price($product);
$prd_wrapper = entity_metadata_wrapper('commerce_product', $form['product_id']['#value']);
$field_price_type = $prd_wrapper->field_prijs_type->value();
$weight_array = array(
'0.1' => '100 gram ' . commerce_currency_format(0.1 * $price['amount'], $price['currency_code'], $product),
'0.2' => '200 gram ' . commerce_currency_format(0.2 * $price['amount'], $price['currency_code'], $product),
'0.3' => '300 gram ' . commerce_currency_format(0.3 * $price['amount'], $price['currency_code'], $product),
'0.4' => '400 gram ' . commerce_currency_format(0.4 * $price['amount'], $price['currency_code'], $product),
'0.5' => '500 gram ' . commerce_currency_format(0.5 * $price['amount'], $price['currency_code'], $product),
'0.6' => '600 gram ' . commerce_currency_format(0.6 * $price['amount'], $price['currency_code'], $product),
'0.7' => '700 gram ' . commerce_currency_format(0.7 * $price['amount'], $price['currency_code'], $product),
'0.8' => '800 gram ' . commerce_currency_format(0.8 * $price['amount'], $price['currency_code'], $product),
'0.9' => '900 gram ' . commerce_currency_format(0.9 * $price['amount'], $price['currency_code'], $product),
'1' => '1 KG ' . commerce_currency_format(1 * $price['amount'], $price['currency_code'], $product)
);
$piece_array = array(
'0.1' => '1 stuks ' . commerce_currency_format(1 * $price['amount'], $price['currency_code'], $product),
'0.2' => '2 stuks ' . commerce_currency_format(2 * $price['amount'], $price['currency_code'], $product),
'0.3' => '3 stuks ' . commerce_currency_format(3 * $price['amount'], $price['currency_code'], $product),
'0.4' => '4 stuks ' . commerce_currency_format(4 * $price['amount'], $price['currency_code'], $product),
'0.5' => '5 stuks ' . commerce_currency_format(5 * $price['amount'], $price['currency_code'], $product),
'0.6' => '6 stuks ' . commerce_currency_format(6 * $price['amount'], $price['currency_code'], $product),
'0.7' => '7 stuks ' . commerce_currency_format(7 * $price['amount'], $price['currency_code'], $product),
'0.8' => '8 stuks ' . commerce_currency_format(8 * $price['amount'], $price['currency_code'], $product),
'0.9' => '9 stuks ' . commerce_currency_format(9 * $price['amount'], $price['currency_code'], $product),
'1' => '10 stuks ' . commerce_currency_format(10 * $price['amount'], $price['currency_code'], $product)
);
if ($field_price_type == 'Prijs per kilo') {
$select = $weight_array;
}
if ($field_price_type == 'Prijs per stuk') {
$select = $piece_array;
}
$form['quantity']['#type'] = 'select';
$form['quantity']['#options'] = $select;
$form['quantity']['#size'] = 1;
}
}
I want to do the same thing more or less on the Shopping Cart page. However, this is a view and therefore my code is not working over there.
I have tried some code but its not working at all, and I think I'm looking at it the wrong way.
This is what I have for the moment.
function kmsol_customcart_commerce_cart_order_load($uid = 0)
{
global $user;
$order = commerce_cart_order_load($user->uid);
dpm($uid);
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
foreach ($order_wrapper->commerce_line_items as $line_item => $line_item_wrapper)
{
$product = $line_item_wrapper->commerce_product->value();
$field_price_type = $product->field_prijs_type->value();
dpm($field_price_type);
$weight_array = array(
'0.1' => '100 gram ' ,
'0.2' => '200 gram ' ,
'0.3' => '300 gram ' ,
'0.4' => '400 gram ' ,
'0.5' => '500 gram ' ,
'0.6' => '600 gram ' ,
'0.7' => '700 gram ' ,
'0.8' => '800 gram ' ,
'0.9' => '900 gram ' ,
'1' => '1 KG '
);
$piece_array = array(
'0.1' => '1 stuks ' ,
'0.2' => '2 stuks ' ,
'0.3' => '3 stuks ' ,
'0.4' => '4 stuks ' ,
'0.5' => '5 stuks ' ,
'0.6' => '6 stuks ' ,
'0.7' => '7 stuks ' ,
'0.8' => '8 stuks ' ,
'0.9' => '9 stuks ' ,
'1' => '10 stuks '
);
if ($field_price_type == 'Prijs per kilo') { $select = $weight_array; }
if ($field_price_type == 'Prijs per stuk') { $select = $piece_array; }
$form['edit_quantity']['#type'] = 'select';
$form['edit_quantity']['#options'] = $select;
$form['edit_quantity']['#size'] = 1;
}
}
The issue is that the quantity widget will be filled based upon the product type, so grams will remain grams and units stay units.