-3

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.

4
  • 2
    What are you trying to do ?
    – CamelCode
    Mar 4, 2016 at 19:48
  • Well I have a products with 2 kind of prices, price per unit and price per weight. I have a field (field_prijs_type) which is a radio button. So when I add a product to the webshop, my quantity textbox becomes a drop down. The if thing is not working because tje syntax is wrong I guess.. Is this clear?
    – user58642
    Mar 4, 2016 at 20:16
  • 1
    Better to add such details in your question (edit it and be as detailed as possible), then request moderators to reopen the question. As for a push in the right direction. Drupal radio options (as any other option on a multi-value field have a system name and a user friendly name). You should be looking for the system name. Further $form_state['values'] is only available after form submission or after an AJAX callback. I see neither of those in your code so it is probably not (yet) defined. Mar 4, 2016 at 20:56
  • I have changed my opening post to be more clear now. My first issue is resolved meanwhile, I'm only having issues with the cart view. Would you reconsider opening this topic? @kiamlaluno thanks in advance
    – J. Mi
    Mar 6, 2016 at 9:10

2 Answers 2

1

I found the solution So i'll share it if someone else needs it..


hook_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(
            '1' => '1 stuks ' . commerce_currency_format(1 * $price['amount'], $price['currency_code'], $product),
            '2' => '2 stuks ' . commerce_currency_format(2 * $price['amount'], $price['currency_code'], $product),
            '3' => '3 stuks ' . commerce_currency_format(3 * $price['amount'], $price['currency_code'], $product),
            '4' => '4 stuks ' . commerce_currency_format(4 * $price['amount'], $price['currency_code'], $product),
            '5' => '5 stuks ' . commerce_currency_format(5 * $price['amount'], $price['currency_code'], $product),
            '6' => '6 stuks ' . commerce_currency_format(6 * $price['amount'], $price['currency_code'], $product),
            '7' => '7 stuks ' . commerce_currency_format(7 * $price['amount'], $price['currency_code'], $product),
            '8' => '8 stuks ' . commerce_currency_format(8 * $price['amount'], $price['currency_code'], $product),
            '9' => '9 stuks ' . commerce_currency_format(9 * $price['amount'], $price['currency_code'], $product),
            '10' => '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;
        $form['quantity']['#default_value'] = 0.1;
    }
} 

// Cart page
if (strpos($form_id, 'views_form_commerce_cart_form_default') === 0) 
{
    //dpm($form_id);
    //dpm($form);
    //dpm($form_state);

    if (isset($form['edit_quantity'])) {            
        foreach ($form['edit_quantity'] as $key => $quantity_field) {               
            if (is_array($quantity_field)) {                    
                // query the DB for each line item in order to get the product id
                $entity_type = 'commerce_line_item';
                $query = new EntityFieldQuery();
                $query->entityCondition('entity_type', $entity_type, '=')->propertyCondition('line_item_id', $quantity_field['#line_item_id'], '=');
                $result = $query->execute();

                if (!empty($result[$entity_type])) {
                    // load the full line item information
                    $line_item = entity_load($entity_type, array_keys($result[$entity_type]));

                    // get the options array if a select box is necessary
                    $product = $line_item[$quantity_field['#line_item_id']]->commerce_product['und'][0]['product_id'];   
                    $prd_wrapper = entity_metadata_wrapper('commerce_product', $product);
                    $field_price_type = $prd_wrapper->field_prijs_type->value();

                    $weight_array = array(
                        '0.10' => '100 gram ' ,
                        '0.20' => '200 gram ' ,
                        '0.30' => '300 gram ' ,
                        '0.40' => '400 gram ' ,
                        '0.50' => '500 gram ' ,
                        '0.60' => '600 gram ' ,
                        '0.70' => '700 gram ' ,
                        '0.80' => '800 gram ' ,
                        '0.90' => '900 gram ' ,
                        '1.00' => '1 KG  '
                    );
                    $piece_array  = array(
                        '1.00' => '1 stuks ' ,
                        '2.00' => '2 stuks ' ,
                        '3.00' => '3 stuks ' ,
                        '4.00' => '4 stuks ' ,
                        '5.00' => '5 stuks ' ,
                        '6.00' => '6 stuks ' ,
                        '7.00' => '7 stuks ' ,
                        '8.00' => '8 stuks ' ,
                        '9.00' => '9 stuks ' ,
                        '10.00' => '10 stuks ' 
                    );  

                    if ($field_price_type == 'Prijs per kilo') {
                        $select = $weight_array;
                    }
                    if ($field_price_type == 'Prijs per stuk') {
                        $select = $piece_array;
                    }
                    //dpm($line_item);
                    //dpm($line_item[$quantity_field['#line_item_id']]->quantity);

                    $form['edit_quantity'][$key]['#type']           = 'select';
                    $form['edit_quantity'][$key]['#options']        = $select;
                    $form['edit_quantity'][$key]['#size']           = 1;
                    $form['edit_quantity'][$key]['#default_value']  = $line_item[$quantity_field['#line_item_id']]->quantity;
                }

            }
        }
    }
}

}


0

Streamlining the above answer. To switch quantity textbox to be a select field...

function THEME_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id){
    case 'views_form_commerce_cart_form_default':
      $select = array('1' => '1 ','2' => '2 ','3' => '3 ','4' => '4 ','5' => '5 ','6' => '6 ','7' => '7 ','8' => '8 ','9' => '9 ');

      foreach ($form['edit_quantity'] as $key => $quantity_field) {
        $form['edit_quantity'][$key]['#type']           = 'select';
        $form['edit_quantity'][$key]['#options']        = $select;
        $form['edit_quantity'][$key]['#size']           = 1;
        $form['edit_quantity'][$key]['#default_value']  =    $line_item[$quantity_field['#line_item_id']]->quantity;
      }
      break;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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