function dc_ajax_add_cart_ajax_cart_form in Commerce Ajax Add to Cart 7.2
Same name and namespace in other branches
- 7 dc_ajax_add_cart.module \dc_ajax_add_cart_ajax_cart_form()
AJAX-ify the product add to cart.
AJAX callback for all Commerce add to cart form.
1 string reference to 'dc_ajax_add_cart_ajax_cart_form'
- dc_ajax_add_cart_form_alter in ./
dc_ajax_add_cart.module - Implements hook_form_alter().
File
- ./
dc_ajax_add_cart.module, line 207 - Ajax add to cart module.
Code
function dc_ajax_add_cart_ajax_cart_form(&$form, &$form_state) {
// AJAX commands array.
$commands = array();
$errors = form_get_errors();
// Get the current status of commerce cart.
$commerce_cart = dc_ajax_add_cart_get_commerce_cart_details();
// If the user has ordered items.
if ($commerce_cart['order'] && !$errors) {
// Get the line items in cart with their quantity and total.
$line_items = $commerce_cart['wrapper']->commerce_line_items;
$quantity = commerce_line_items_quantity($line_items, commerce_product_line_item_types());
$total = commerce_line_items_total($line_items);
$ajax_shopping_cart_content = theme('dc_ajax_shopping_cart', array(
'order' => $commerce_cart['order'],
'line_items' => $line_items,
'quantity' => $quantity,
'total' => $total,
));
$commands[] = dc_ajax_add_cart_command_html('div.ajax-shopping-cart-wrapper', $ajax_shopping_cart_content);
// Update the contents of shopping cart.
$ajax_shopping_cart_teaser_content = theme('dc_ajax_shopping_cart_teaser', array(
'order' => $commerce_cart['order'],
'quantity' => $quantity,
'total' => $total,
));
$commands[] = dc_ajax_add_cart_command_html('div.ajax-shopping-cart-teaser', $ajax_shopping_cart_teaser_content);
// Display add to cart message.
if (variable_get(DC_AJAX_ADD_CART_DISPLAY_POPUP, 'display_popup_message') == 'display_popup_message') {
// Gather information to display product information in popup message.
$last_line_item = dc_ajax_add_cart_get_last_line_item_wrapper($line_items);
$last_line_item_product = commerce_product_load($last_line_item->commerce_product[LANGUAGE_NONE][0]['product_id']);
$last_line_item_quantity = $form_state['values']['quantity'];
$content = theme('dc_ajax_add_to_cart_message', array(
'line_item' => $last_line_item,
'product' => $last_line_item_product,
'quantity' => $last_line_item_quantity,
));
$commands[] = ajax_command_prepend('body', $content);
}
}
// Clear status messages if any.
$commands[] = ajax_command_remove('div.messages');
// Show Drupal status message without page refresh.
// @see https://www.drupal.org/node/1271004#comment-7205478
// We are checking the textual status messages so that we can save double
// calling of drupal_get_messages().
$messages = theme('status_messages');
if (!empty($messages)) {
$commands[] = ajax_command_before('#main-content', $messages);
}
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}