0

Thanks to the help I received in my previous question, I was able to change the HTML of shipping service from radios to a select field. This was done, as suggested, by means of a custom module implementing hook_form_alter.

function MYMODULE_form_alter(&$form, &$form_state, $form_id){
    if ($form_id=='commerce_checkout_form_checkout') {
        $form['commerce_shipping']['shipping_service']['#type']='select';
    }
}    

Now what i need is to have an empty option ("select the proper value") in it, so that the user would be forced to actively choose one of the shipping methods. What I get from

print_r($form);

is

    [shipping_service] => Array    
                (    
                [#type] => radios    
                [#options] => Array    
                    (    
                        [deli] => Delivery : 5,00    
                        [pickup] => Pickup: 0,00
                    )
                [#ajax] => Array
                    (
                        [callback] => commerce_shipping_pane_service_details_refresh
                        [wrapper] => commerce-shipping-service-details
                    )

                [deli] => Array
                    (
                        [#description] => 
                    )

                [pickup] => Array
                    (
                        [#description] => 
                    )

                [#default_value] => deli
            )

        [service_details] => Array
            (
                [#type] => container
                [#prefix] => 

                [#suffix] => 

            )         

I tried to tinker with dafault value, inserting another option, like

$form['commerce_shipping']['shipping_service']['#options']['_none']='['-select-']';
$form['commerce_shipping']['shipping_service']['#options']['#default_value']='['_none']';

(which was somewhat randomly inspired by the 'print_r' output from a taxonomy term field on the same form but brought no results and did cause AJAX errors. Any ideas?

6
  • Any reason you haven't tried to simply unset default value?
    – Mołot
    Commented Nov 30, 2014 at 17:49
  • @Mołot I tried unset($form['commerce_payment']['payment_method']['#default_value']); which does not bring the desired result - actually, it does not change anything.
    – Dalmaz
    Commented Nov 30, 2014 at 18:08
  • @Mołot : I also need the user to see the choice's not been made, so i need to enter a 'please choose' position.
    – Dalmaz
    Commented Nov 30, 2014 at 18:42
  • There is a parsing error in $form['commerce_shipping']['shipping_service']['#options']['#default_value']='['_none']';. The value is then '_none', not '[_none]'.
    – avpaderno
    Commented Dec 1, 2014 at 6:59
  • @kiamlaluno so it chould be $form['commerce_shipping']['shipping_service']['#options']['#default_value']=['none']?
    – Dalmaz
    Commented Dec 1, 2014 at 9:16

1 Answer 1

1

The documentation says:

If #required is TRUE and there is no #default_value, an empty option is added to the select control to force the user to make an active choice.

So take the default value out and make sure that the form code includes '#required' => TRUE,

I also read somewhere that you must use drupal_get_form() to build the output for a form otherwise the additional form processing functions will not run. I did not use hook_form_alter() to modify shipping services but instead used hook_commerce_shipping_service_info() in a custom module to give me more flexibility.

2
  • Thanks! This seems promising, as the code gets simpler and more efficient. I erased the array modifications, added #required, and the empty value presents itsef. I still do get AJAX errors when changing the select box value.
    – Dalmaz
    Commented Dec 2, 2014 at 10:24
  • ...the AJAX errors turned out to be the effect of printing the variables to the page by AJAX! After disabling the print_r() that i used to collect the information about the arrays, they were gone. I still cannot achieve functioning checkout without the default payment/shipping selection - AJAX fails to let me past the checkout block
    – Dalmaz
    Commented Dec 2, 2014 at 20:04

Your Answer

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

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