Hello, I have the following problem with radios buttons:

1.- I've changed $form['attributes']['product_select']['#type'] to radios:

<?php
function MYODULE_form_alter(&$form, &$form_state, $form_id) {
  if (
strpos($form_id, 'commerce_cart_add_to_cart_form_') === 0 && isset($form['attributes']['product_select'])) {
   
$form['attributes']['product_select']['#type'] = "radios";
  }
}
?>

2.- I've got products with taxonomy attributes in a display
3.- Load in browser the product display with 3 products and multiples attributes. All OK.
4.- Select an attribute and all radios of select product deselects.
5.- Select other attribute and again all is well. First radios is selected.
6.- Select an attribute and all radios of select product are deselected.
7.- Select other attribute and again all is well. First radios is selected.
8.- And so it goes... Radios buttons of products are selected / deselected alternately on each attribute selection.

Attached some pictures for a better understanding.
3.- Load in browser the product display with 3 products and multiples attributes. All OK.
Firs load
4.- Select an attribute and all radios of select product deselects.
Firs selected
5.- Select other attribute and again all is well. First radios is selected.
Second selected

Any idea about this?
Thanks

Files: 
CommentFileSizeAuthor
3.png9.94 KBfacine
2.png10.51 KBfacine
1.png11.4 KBfacine

Comments

Do you mind sharing out your custom module? :)

Gonna need a little more clarification; I would expect it to choose a default product, for sure, but I'm going to have to be able to recreate your scenario locally to really test it. Can you provide a sample list of product information I can use to recreate your product page? Also, are you only using Commerce 1.0 instead of 1.1?

For example: 3 Products with attributes taxonomy in one product display.
Attributes: 4GB, 8GB, 16GB.
products:
(*) IPod
() IPhone
() IPad
Change the attribute of 4GB to 8GB and update the display all the products are deselected.
() IPod
() IPhone
() IPad
I go back to change the attribute from 8GB to 16GB and re-update the display returns to be selected the first radio.
(*) IPod
() IPhone
() IPad
I go back to change the attribute to 8GB 16GB and update the display again all products are deselected.
() IPod
() IPhone
() IPad

Default product is selected/deselected/selected/deselected on every display refresh.

Ok, thanks. I'll give it a go when I get a few minutes.

Ok, thanks!

Version:7.x-1.0» 7.x-1.x-dev

Just pointing out that I can duplicate this behavior when I change it to use radios, but what's curious is that with a little debug message in there, it appears it's determining the default product just fine. For example, even if I change it and get the radios element with no default option selected, I can still click "Add to Cart" and get the correct default product add to the cart. Curious indeed. I haven't the slightest clue how to debug this.

Title:Ajax Callback problem with attributes and product type select changed as radios with form alter"Select a product" element is ignoring its new #default_value on AJAX refresh
Status:Active» Fixed

Ok, so examining theme_radio(), I found out the problem. Drupal only sets the checked attribute on a radio button if its #return_value matches the element's #value:

<?php
 
if (isset($element['#return_value']) && $element['#value'] !== FALSE && $element['#value'] == $element['#return_value']) {
?>

The problem of course is that this is an AJAX refresh where the #value is being retained from a now invalid set of #options, because that's what got submitted when the AJAX request was made. We had this same problem with attribute form elements, and of course we got around it for them by simply unsetting their values from the $form_state['input'] array. The solution here is the same - if I detect that attributes have changed, which may result in a new set of #options for the "Select a product" element (if it even exists on the form), then I am also unsetting the $form_state['input']['attributes']['product_select'] value so whatever was submitted from the last set of #options will be ignored.

This problem actually existed whether you were using a select list or radio buttons, but with a select list it still defaulted to the first available option since there is no "- None -" option in the select list by default.

On a side note: remember that if you change this element to radio buttons list instead of a select list, you are responsible to sanitize the product titles used in the #options array per the comments in lines 1791-1796.

Commit: http://drupalcode.org/project/commerce.git/commitdiff/c8091b0

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.