function commerce_option_field_attach_submit in Commerce Product Option 7.2
Implements hook_field_attach_submit().
This hook gets called in field_attach_submit(), which is called by commerce_cart_add_to_cart_form_submit(). It is here we need to add the option set property on the commerce_line_item entity. We do it here because here we have access to $form_state, which holds the values for the option set.
File
- ./
commerce_option.module, line 710
Code
function commerce_option_field_attach_submit($entity_type, $entity, $form, &$form_state) {
if ($entity_type != 'commerce_line_item') {
return;
}
// Get current product, taking into account different Commerce versions behavior.
if (isset($form_state['default_product'])) {
$product_id = $form_state['default_product']->product_id;
}
elseif (isset($form_state['default_product_id'])) {
$product_id = $form_state['default_product_id'];
}
elseif (isset($form_state['products'])) {
$current_product = reset($form_state['products']);
$product_id = $current_product->product_id;
}
else {
return;
}
$current_product = commerce_product_load($product_id);
// Iterates the fields of this product. We search for entity reference fields
// to a commerce option set.
foreach ($current_product as $field_name => $field) {
$field_info = field_info_field($field_name);
if ($field_info['type'] != 'entityreference' || !isset($field_info['settings']['target_type']) || $field_info['settings']['target_type'] != 'commerce_option_set') {
continue;
}
// Do not do anything if this field has been marked as an attribute field.
$field_instance_info = field_info_instance('commerce_product', $field_name, $current_product->type);
if ($field_instance_info['commerce_cart_settings']['attribute_field']) {
return;
}
$lang_code = field_language('commerce_product', $current_product, $field_name);
if (!isset($field[$lang_code])) {
continue;
}
foreach ($field[$lang_code] as $delta => $set_id) {
$option = $form_state['commerce_option'][$field_name][$delta]['option'];
// Notify field widgets. Look in complete form subarray because our
// original fields in the $form array tend to disappear.
field_attach_submit('commerce_option', $option, $form_state['complete form'][$field_name][$delta], $form_state);
$options_extracted = commerce_option_get_valuables($option);
$entity->{$option->set_id} = serialize($options_extracted);
}
}
}