You are here

function dc_ajax_add_cart_update_quantity_refresh in Commerce Ajax Add to Cart 7

Same name and namespace in other branches
  1. 7.2 dc_ajax_add_cart.module \dc_ajax_add_cart_update_quantity_refresh()

Callback that will update the quantity of line item.

1 string reference to 'dc_ajax_add_cart_update_quantity_refresh'
dc_ajax_add_cart_update_quantity in ./dc_ajax_add_cart.module
Update line item quantity.

File

./dc_ajax_add_cart.module, line 718
Ajax add to cart module.

Code

function dc_ajax_add_cart_update_quantity_refresh($form, &$form_state) {
  global $user;
  $order = commerce_cart_order_load($user->uid);

  // AJAX commands array.
  $commands = array();

  // If altered quantity is not zero, then update line item's quantity.
  // Otherwise delete line item.
  if ($form_state['values']['quantity_' . $form_state['triggering_element']['#line_item_id']] != 0) {
    $line_item = commerce_line_item_load($form_state['triggering_element']['#line_item_id']);
    $line_item->quantity = $form_state['values']['quantity_' . $form_state['triggering_element']['#line_item_id']];
    commerce_line_item_save($line_item);
  }
  else {
    commerce_cart_order_product_line_item_delete($order, $form_state['triggering_element']['#line_item_id']);
  }

  // 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);

    // Check whether the user has any product in cart.
    if (variable_get(DC_AJAX_ADD_CART_HIDE_EMPTY_CART, 0) && $quantity == 0) {
      $ajax_shopping_cart_content = '';
    }
    else {
      $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);
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
  }
}