0

I'm using Commerce Kickstart and I have a term reference for Size field of my product variation, and I'm using the Check boxes/radio buttons widget with commerce fancy attributes.

Currently, when I'm in the Product Display page, it seems that the default value set to Large. Since I don't want to insult my users by appearing to think that they are all "Large", I would like to set the default value to "S"

I'm using the following code to target all the commerce add to cart forms, with reference to an answer from here: Commerce - Theme Add-to-Cart form on Product Display page

function MODULENAME_form_alter(&$form, &$form_state, $form_id){
  // Add to cart form overrides
  if(substr($form_id, 0, 30) == 'commerce_cart_add_to_cart_form'){
    $form['attributes']['field_size']['#default_value'] = VALUE
    $form['unchanged_attributes']['field_size']['#value'] = '13';
    //where VALUE is chosen from $form['attributes']['field_size']['#options']
  }
}

Despite the fact that Small is highlighted as a result, when I press "Add to cart", size "L" gets added to the cart by default.

(When I click a value, such as "M", size "M" gets added to cart as expected, it is only the default value that is buggy)

enter image description here

enter image description here

This is my dpm($form) for reference.

screenshot

7
  • Just keep the value required when you create the field and remove the default value if it is there Commented Aug 6, 2014 at 5:23
  • I am not sure what problem you are seeing. The default value I see set in the second screenshot is 13, which is the value associated to S. What are you expecting to see as default value? S? If that is the case, then it is not how Drupal works
    – apaderno
    Commented Aug 10, 2014 at 10:29
  • Also, it is the Devel module that writes (callback) L(), but the #options values are not callbacks. They are just strings that appear in the UI; they are not taken as being callbacks, from Drupal.
    – apaderno
    Commented Aug 10, 2014 at 10:32
  • I am going to close this question until you don't make clear what you mean by callback value in "the callback value is not changed." There aren't callbacks involved, in the code you are showing.
    – apaderno
    Commented Aug 10, 2014 at 10:36
  • Actually my real problem was that when I press "Add to cart", size "L" gets added to the cart by default, despite the fact that the default value was set to 13 or S. I thought that it was due to the callback but I was merely guessing. I appreciate any help you may offer me that would help me understand this problem. Thanks! (When I click a value, such as "M", size "M" gets added to cart as expected, it is only the default value that is buggy). I have edited my question.
    – ashash
    Commented Aug 10, 2014 at 16:43

0

Your Answer

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

Browse other questions tagged or ask your own question.