function dc_ajax_add_cart_ajax_cart_form in Commerce Ajax Add to Cart 7
Same name and namespace in other branches
- 7.2 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 200 - Ajax add to cart module.
Code
function dc_ajax_add_cart_ajax_cart_form(&$form, &$form_state) {
$form_errors = form_get_errors();
// AJAX commands array.
$commands = array();
if (!empty($form_errors)) {
// Show Drupal status message without page refresh.
// @see https://www.drupal.org/node/1271004#comment-7205478
$commands[] = ajax_command_remove('div.messages');
$commands[] = ajax_command_before('#main-content', theme('status_messages'));
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
// 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']) {
// 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 = $form_state['line_item'];
$last_line_item_product = commerce_product_load($form_state['values']['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');
return array(
'#type' => 'ajax',
'#commands' => $commands,
);
}
}