I'm using hook_form_alter to add in 'Please select' to the product attributes (sizes and colours) available. However, this kills the ajax updating! In other words, available sizes aren't reloaded when a user chooses a different colour. If I can't alter the choices via hook_form_alter, is there some other better way to be doing this?

Comments

bojanz’s picture

Status: Active » Postponed (maintainer needs more info)

Please share your code. You're probably doing it incorrectly, accidentally killing #ajax on the form element.

patrickharris’s picture

I'm trying to have 'Please select' when the product is first loaded. Here is the code.

/**
 * Implements hook_form_alter().
 */
function MODULE_form_alter(&$form, &$form_state, $form_id) {
	if($form_id == 'commerce_product_ui_product_form') {
	if(isset($form['attributes']['field_size']['#options']) && count($form['attributes']['field_size']['#options']) > 1) {
		$form['attributes']['field_size']['#options'] = array('0' => 'Please select') + $form['attributes']['field_size']['#options'];
       $form['attributes']['field_size']['#default_value'] = 0;
	   $form['#validate'][] = 'gv_dont_allow_no_size_choice';
	}
  }
}

The validation function works and causes no problems, but adding in the extra option 'Please select' stops the ajax working. Why would this be? Any ideas? Is there a better way to do this than hook_form_alter?

rszrama’s picture

Category: bug » support
Status: Postponed (maintainer needs more info) » Fixed

Yeah, really you can't do this because the form depends on being able to determine based on the currently selected values what product should be represented on the form. In other words, it needs each attribute to have a value, not an empty "Please select" option.

You have two recourses: 1) use JS / the theme layer to show your "Please select" text without actually changing the value of the field or 2) further alter the form so the "Please select" value is actually equated to the otherwise default value for the widget.

Status: Fixed » Closed (fixed)

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

patrickharris’s picture

Thanks Ryan!!