function commerce_product_attributes_add_to_cart_form_submit in Commerce Product Attributes 7
Submit callback function for the add to cart function.
1 string reference to 'commerce_product_attributes_add_to_cart_form_submit'
- commerce_product_attributes_form_alter in ./
commerce_product_attributes.module - Implementation of hook_form_alter()
File
- ./
commerce_product_attributes.module, line 259 - This module adds some improvements to the Drupal Commerce core.
Code
function commerce_product_attributes_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, then
// use the default handler.
if (empty($form_state['line_item']->line_item_id)) {
commerce_cart_add_to_cart_form_submit($form, $form_state);
}
else {
$order = commerce_order_load($form_state['line_item']->order_id);
// Remove the line item.
commerce_cart_order_product_line_item_delete($order, $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);
// 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'] = 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);
// TODO: Accommodate multiple product Add to Cart forms better; i.e. should it
// display the product title or the product display node title?
drupal_set_message(t('%title is updated in <a href="!cart-url">your cart</a>.', array(
'%title' => $product->title,
'!cart-url' => url('cart'),
)));
}
else {
drupal_set_message(t('%title could not be updated.', array(
'%title' => $product->title,
)), 'error');
}
}
}