Hello, I have the following problem with radios buttons:
1.- I've changed $form['attributes']['product_select']['#type'] to radios:
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.
4.- Select an attribute and all radios of select product deselects.
5.- Select other attribute and again all is well. First radios is selected.
Any idea about this?
Thanks
Comments
Comment #1
jessicakoh CreditAttribution: jessicakoh commentedDo you mind sharing out your custom module? :)
Comment #2
rszrama CreditAttribution: rszrama commentedGonna 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?
Comment #3
facine CreditAttribution: facine commentedFor 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.
Comment #4
rszrama CreditAttribution: rszrama commentedOk, thanks. I'll give it a go when I get a few minutes.
Comment #5
facine CreditAttribution: facine commentedOk, thanks!
Comment #6
rszrama CreditAttribution: rszrama commentedJust 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.
Comment #7
rszrama CreditAttribution: rszrama commentedOk, 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:
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
Comment #9
konordo CreditAttribution: konordo commentedrelevant: http://drupal.org/node/1364510