function i18n_commerce_product_commerce_cart_add_to_cart_form_submit in Internationalization for commerce product 7
Form submit handler: add the selected product to the cart.
1 string reference to 'i18n_commerce_product_commerce_cart_add_to_cart_form_submit'
- i18n_commerce_product_form_alter in ./
i18n_commerce_product.forms.inc - Implements hook_form_alter().
File
- ./
i18n_commerce_product.forms.inc, line 184 - Internationalization (i18n) module - Commerce product handling.
Code
function i18n_commerce_product_commerce_cart_add_to_cart_form_submit($form, &$form_state) {
$product_id = $form_state['values']['product_id'];
$product = commerce_product_load($product_id);
// If the line item passed to the function is new...
if (empty($form_state['line_item']->line_item_id)) {
// Create the new product line item of the same type.
$line_item = commerce_product_line_item_new($product, $form_state['values']['quantity'], 0, $form_state['line_item']->data, $form_state['line_item']->type);
// Allow modules to prepare this as necessary. This hook is defined by the
// Product Pricing module.
drupal_alter('commerce_product_calculate_sell_price_line_item', $line_item);
// Remove line item field values the user didn't have access to modify.
foreach ($form_state['values']['line_item_fields'] as $field_name => $value) {
// Note that we're checking the Commerce Cart settings that we inserted
// into this form element array back when we built the form. This means a
// module wanting to alter a line item field widget to be available must
// update both its form element's #access value and the field_access value
// of the #commerce_cart_settings array.
if (empty($form['line_item_fields'][$field_name]['#commerce_cart_settings']['field_access'])) {
unset($form_state['values']['line_item_fields'][$field_name]);
}
}
// Unset the line item field values array if it is now empty.
if (empty($form_state['values']['line_item_fields'])) {
unset($form_state['values']['line_item_fields']);
}
// Add field data to the line item.
field_attach_submit('commerce_line_item', $line_item, $form['line_item_fields'], $form_state);
// Process the unit price through Rules so it reflects the user's actual
// purchase price.
rules_invoke_event('commerce_product_calculate_sell_price', $line_item);
// Only attempt an Add to Cart if the line item has a valid unit price.
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
if (!is_null($line_item_wrapper->commerce_unit_price
->value())) {
// Add the product to the specified shopping cart.
$form_state['line_item'] = i18n_commerce_product_commerce_cart_product_add($form_state['values']['uid'], $line_item, isset($line_item->data['context']['add_to_cart_combine']) ? $line_item->data['context']['add_to_cart_combine'] : TRUE);
}
else {
drupal_set_message(t('%title could not be added to your cart.', array(
'%title' => $product->title,
)), 'error');
}
}
}